Mixing in-memory and persistent SwiftData containers in a Document-based App?

Hello,

I'm trying to work on an iPadOS and macOS app that will rely on the document-based system to create some kind of orientation task to follow.

Let say task1.myfile will be a check point regulation from NYC to SF and task2.myfile will be a visit as many key location as you can in SF.

The file represent the specific landmark location and rules of the game.

And once open, I will be able to read KML/GPS file to evaluate their score based with the current task.

But opened GPS files does not have to be stored in the task file itself, it stay alongside.

I wanted to use that scenario to experiment with SwiftData (I'm a long time CoreData user, I even wrote my own WebDAV based persistent store back in the day), and so, mix both on file and in memory persistent store, with distribution based on object class.

With CoreData it would have been possible, but I do not see how to achieve that with SwiftData and DocumentGroup integration.

Any idea how to do that?

Please correct me if my understanding is incorrect, but it seems to me that:

  1. Your app handles some document files (.myfile).
  2. Each document file has an associated KML/GPS file.
  3. You need to access the KML/GPS file with your own code when the document file is opened with DocumentGroup.

When using DocumentGroup + SwiftData, the document file is a SwiftData store, and the framework manages the SwiftData model container (hence the store file) and provides a model context via a SwiftUI environment for you to access the data.

With the model context and its model container, you are free to look into the SwiftData store, find the path of the associated KML/GPS file (assuming it is part of your SwiftData store), and access the KML/GPS data in your way (step 3).

If you need to make the document file a bundle package that includes the SwiftData store file and the associated KML/GPS file, that is unsupported by today's DocumentGroup + SwiftData.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Mixing in-memory and persistent SwiftData containers in a Document-based App?
 
 
Q