SwiftData with two Stores

Hi, has anybody managed to get two sqlite stores working? If I define the stores with a configuration for each it seems like that only the first configuration and and therefore the store is recognised. This is how I define the configuration and container:

import SwiftData

@main
struct SwiftDataTestApp: App {

    var modelContainer: ModelContainer
    
    init() {
        let fullSchema = Schema([
            SetModel.self,
            NewsModel.self
        ])
        
        let setConfiguration = ModelConfiguration(
            "setconfig",
            schema: Schema([SetModel.self]),
            url: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("Sets.sqlite"),
            readOnly: false)
        
        let newsConfiguration = ModelConfiguration(
            "newsconfig",
            schema: Schema([NewsModel.self]),
            url: FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent("News.sqlite"),
            readOnly: false)
        
        modelContainer = try! ModelContainer(for: fullSchema, configurations: [setConfiguration,newsConfiguration])
    }
        var body: some Scene {       
            WindowGroup {
                ContentView()
            }
            .modelContainer(modelContainer)
        }
}

ContentView is just a basic TabView with a tab for news and a tab for sets. If I run the program this way the sets tab is shown correctly but switching to News fails. If I change the order of the configurations and write the one for news first like this:

modelContainer = try! ModelContainer(for: fullSchema, configurations: [newsConfiguration, setConfiguration])

then the news tab is shown correctly and switching to sets tab fails. NewsModel and SetModel only differ in the class name

Import Foundation
import SwiftData

@Model
public class NewsModel{
    public var name: String
    init(name: String) {
        self.name = name
    }
}

Also the tab content differs only for referencing the respecting model and the name:

import SwiftData

struct NewsTab: View {
    @Query private var news: [NewsModel]
    @Environment(\.modelContext) private var modelContext
    var body: some View {
        ScrollView{
            LazyVStack{
                ForEach(news){actNews in
                    Text("Hello, News \(actNews.name)")
                }
            }
            .onAppear {
                let news = NewsModel(name: "News from \(Date())")
                modelContext.insert(news)
                try! modelContext.save()
            }
        }
    }
}

The error message is "NSFetchRequest could not locate an NSEntityDescription for entity name 'NewsModel'" (and SetsModel respectively when change the order of the configuration)

Do I explicitly need to tell the modelContext which configuration it should use or is this done automatically?

I'm a little lost here and hope someone can help me.

Best regards, Sven

Answered by DTS Engineer in 778997022

This is an issue on the framework side, and should be fixed in iOS 17.4, which is now beta. If you try with the beta and still see the issue, I suggest that you file a feedback report. If you need to support iOS versions before 17.4, consider working around the issue by creating one container per configuration.

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.

Accepted Answer

This is an issue on the framework side, and should be fixed in iOS 17.4, which is now beta. If you try with the beta and still see the issue, I suggest that you file a feedback report. If you need to support iOS versions before 17.4, consider working around the issue by creating one container per configuration.

SwiftData with two Stores
 
 
Q