SwiftDevJournal / WikiDemo

A multiplatform SwiftUI project demonstrating various SwiftUI features.
11 stars 3 forks source link

How to save/open a core data file in the "package" file #2

Open duncangroenewald opened 2 years ago

duncangroenewald commented 2 years ago

Is it possible to create/open a core data file from this Package ?

In the init() function could one create an NSPersistentContainer in the main directory and then have the MOC as a variable on the FileDocument ?

Similarly in the fileWrapper() function could one just call the MOC.save() method.

I am guess this seems to easy - unless perhaps one creates an in memory database in init() and then writes that to file in fileWrapper()

Any thoughts on whether this might be feasible ?

SwiftDevJournal commented 2 years ago

I have not tried using file wrappers with Core Data. I don't think SwiftUI's document model works well with Core Data. When you create a SwiftUI Document App project in Xcode, there is no checkbox to use Core Data.

I did a search for NSFileWrapper Core Data and did not find much. I know that Core Data lets you create your own persistent store types. You might be able to create a NSAtomicStore or NSIncrementalStore subclass that saves the data in a file wrapper.