Cannot create ModelContainer for SwiftData on iCloud

589 Views Asked by At

I have two following models created in my SwiftData project in Xcode

@Model
class Notebook {

    var id: String = ""
    var name: String = ""
    var desc: String?
    var notes: [Note]?

    init(id: String, name: String, desc: String? = nil) {
        self.id = id
        self.name = name
        self.desc = desc
    }
}

and

@Model
class Note {
    var id: String = ""
    var created: Date = Date()
    var updated: Date?
    var text: String = ""
    var notebook: Notebook  

    init(id: String, created: Date, updated: Date? = nil, text: String, notebook: Notebook) {
        self.id = id
        self.created = created
        self.updated = updated
        self.text = text
        self.notebook = notebook
     }
}

Project is compiled and works fine if CloudKit capability is unchecked but when I allow it and assign container on iCloud then it fails with error

Fatal error: Could not create ModelContainer: SwiftDataError(_error: SwiftData.SwiftDataError._Error.loadIssueModelContainer)

I tried to make notebook property in Note model optional but it didn't help.

Code for ModelContainer creation

var sharedModelContainer: ModelContainer = {
    let schema = Schema([Notebook.self, Note.self])
    let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)

    do {
        return try ModelContainer(for: schema, configurations: [modelConfiguration])
    } catch {
        fatalError("Could not create ModelContainer: \(error)")
    }
}()

I'll be grateful for advice. Thanks.

0

There are 0 best solutions below