How to add multiple ModelConfigurations to a ModelContainer?

Anyone successfully able to add two or more ModelConfigurations to a ModelContainer?

DESCRIPTION OF PROBLEM:

When you create two ModelConfigurations for two different models and combine them into one ModelContainer, it seems the @Query fails to find the models. Crashes app with error:

“Thread 1: "NSFetchRequest could not locate an NSEntityDescription for entity name 'NumberModel'"

STEPS TO REPRODUCE

1 - Create a new iOS project.

2 - In ContentView.swift add this code:

import SwiftData
import SwiftUI

struct ContentView: View {
    @Query private var colors: [ColorModel]
    @Query private var numbers: [NumberModel]
    
    var body: some View {
        List {
            ForEach(colors) { color in
                Text(color.name)
            }
            ForEach(numbers) { number in
                Text(number.name)
            }
        }
    }
}

#Preview {
    ContentView()
        .modelContainer(for: [ColorModel.self, NumberModel.self])
}

3 - In App file, add this code:

import SwiftData
import SwiftUI

@Model
class ColorModel {
    var name: String = ""
    init(name: String) {
        self.name = name
    }
}

@Model
class NumberModel {
    var name: String = ""
    init(name: String) {
        self.name = name
    }
}

@main
struct MultipleModelConfigsApp: App {
    private var container: ModelContainer
    
    init() {
        do {
            let config1 = ModelConfiguration(for: ColorModel.self)
            let config2 = ModelConfiguration(for: NumberModel.self)
            
            let container = try ModelContainer(
                for: ColorModel.self, NumberModel.self,
                configurations: config1, config2
            )
            
            self.container = container
        } catch {
            fatalError("ModelContainer creation failed.")
        }
    }
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .modelContainer(container)
        }
    }
}

4 - Now run the app and observe the crash and the error stated above.

VERSION OF XCODE

Version 15.1 (15C65)

FEEDBACK REPORT

FB: FB13504577 (Xcode project attached to FB)

Same issue here. I think it is a valid use case as it is e.g. sometimes necessary to separate models that should be synced via iCloud from only local models

Did you solve this problem? If so, can you share how?

Regards

Is this resolved with Xcode 15.3?

Have you tried to give a name to the configurations?

This seems to work for me:

let schema = Schema([ Item.self ])
let cloudSchema = Schema([CKItem.self])

let cloudConfiguration = ModelConfiguration("cloudConfig", schema: cloudSchema, isStoredInMemoryOnly: false, cloudKitDatabase: .private("iCloud.***.yyy.zzz"))

let modelConfiguration = ModelConfiguration("localConfig", schema: schema, isStoredInMemoryOnly: false, cloudKitDatabase: .none)

do {

  let allSchemas = Schema([Item.self, CKItem.self])
           
  return try ModelContainer(
                for: allSchemas,
                configurations: [modelConfiguration, cloudConfiguration]
            )

If you open your simulator's Application Data folder, you'll find 2 stores: cloudConfig.store and localConfig.store, these are the names of the configurations above.

Only one warning, there cannot be relations between the Schemas, otherwise they will be unified under the same schema.

As an extra validation, Item uses an @Attribute(.unique), which is not valid on iCloud configurations.

How to add multiple ModelConfigurations to a ModelContainer?
 
 
Q