SwiftData with two Stores

Apple Recommended

  • Thanks for information.

    I'll need to support iOS version before 17.4 so I will use two containers (one per conf) but can I use both at same time or should I switch ?

    In the example below, still only first container is loaded...

    var firstContainer: ModelContainer var secondContainer: ModelContainer

    var body: some Scene { WindowGroup { HomeView() } .modelContainer(firstDataContainer) .modelContainer(secondDataContainer) }

Add a Comment

Replies

I'm experiencing the same behavior. When multiple configurations are specified for the container, only entities from the first configuration are accessible.

I am experiencing the same issue even with Xcode Version 15.1 beta (Oct 2023). Is there any known solution? I feel it should work, since it was explained in detail in wwdc2023-10196

I am having the same iss, which I have reported here https://developer.apple.com/forums//thread/740120.

This should be a bug.

By using SwiftDataKit to inspect the information of NSPersistentStoreCoordinator, I found that only the store corresponding to the first configuration is loaded.

// ModelContainer(for: Schema([Item.self,Tag.self]), configurations: [configuration2,configuration1])

let coordinator = context.coordinator
print(coordinator?.persistentStores.count)

// 1

Interestingly, SwiftData creates a separate sqlite file for each configuration.

I suppose this has already been reported to Feedback, and now we can only wait for Apple to resolve this issue.

  • Have the same problem with two separate stores. Models from second config are unavailable and try to access them crashes the app.

    Can’t use SwiftData without this feature, because one store is built-in catalog with read-only option, and other is for user content synced with iCloud.

    Hope will be fixed soon 😔

  • Exact same here. Needed to switch my app to separate stores and now my app doesn't work! Only the first config is loaded.

Add a Comment
  • Thanks for information.

    I'll need to support iOS version before 17.4 so I will use two containers (one per conf) but can I use both at same time or should I switch ?

    In the example below, still only first container is loaded...

    var firstContainer: ModelContainer var secondContainer: ModelContainer

    var body: some Scene { WindowGroup { HomeView() } .modelContainer(firstDataContainer) .modelContainer(secondDataContainer) }

Add a Comment