SwiftDataKit icon indicating copy to clipboard operation
SwiftDataKit copied to clipboard

Support for multiple configurations

Open marwey opened this issue 2 years ago • 10 comments

Hi, will this extension library also work for the case that the SwiftData ModelContainer is created with multiple configurations? I saw test cases implemented using only one configuration?

marwey avatar Feb 20 '24 11:02 marwey

@marwey Theoretically, it is feasible, but currently, SwiftData has a bug where only the first configuration works, making it impossible to implement multiple stores within one container, similar to Core Data.

fatbobman avatar Feb 20 '24 11:02 fatbobman

@fatbobman Thanks for the heads up! Yes, looks like this part is buggy. The funny thing is, when I peek into my modelContext with the debugger I can see the data of both of my configurations?

marwey avatar Feb 20 '24 12:02 marwey

@marwey Yes, This is a very interesting bug. SwiftData creates a corresponding SQLite file for each configuration, but it only loads the first configuration (corresponding to NSPersistentStoreDescription in Core Data).

I mentioned it in my twitter https://twitter.com/fatbobman/status/1741820272964251751

fatbobman avatar Feb 20 '24 12:02 fatbobman

@fatbobman Yes, that is inline with what I see in my code. And it's actually independent from your library.

Will file a bug report with them and see if I can work around this in my app.

marwey avatar Feb 20 '24 12:02 marwey

@fatbobman It seems Apple has fixed the above bug. At least the sample I attached to my bug report works in macOS 14.4.

marwey avatar Apr 02 '24 08:04 marwey

@marwey Thanks, I just test it , Yes, It works finally!

fatbobman avatar Apr 02 '24 09:04 fatbobman

@fatbobman My testing under macOS 15 suggests that this kit is broken. I guess Apple removed _nsContext from ModelContext? Do you see any workarounds for this?

marwey avatar Oct 16 '24 20:10 marwey

@marwey 😭

Due to adjustments in the storage logic, SwiftDataKit is no longer applicable to SwiftData following WWDC 2024. For more details, please read SwiftData in WWDC 2024: The Revolution Continues, Stability Still Awaits.

image

fatbobman avatar Oct 16 '24 23:10 fatbobman

@fatbobman Many thanks for confirming and dive deep into this in the attached article. Very valuable!

I guess for dynamic queries I can transform my code to SwiftData. I am concerned about batch inserts. When I tried with SwiftData it was very slow. The Core Data API was so much faster. Any best practices you can share on that one?

marwey avatar Oct 17 '24 12:10 marwey

@marwey you can use Core Data Batch Operation in a SwiftData project. but , you need to handle relationships manually. // code snippet https://gist.github.com/fatbobman/17334937ecf6772fa3586fc2ba1fc852

https://fatbobman.com/en/posts/batchprocessingincoredata/

fatbobman avatar Oct 17 '24 23:10 fatbobman