swiftUI apps with SwiftData and CloudKit crashes on iOS but works on MacOS

sample code


let modelConfiguration = ModelConfiguration()

// 1. create mom
guard let mom = NSManagedObjectModel.makeManagedObjectModel(for: [Attachment.self]) else {
  fatalError("====> makeManagedObjectModel error")
}

// 2. create description with config url and setting
let description = NSPersistentStoreDescription(url: modelConfiguration.url);
description.shouldAddStoreAsynchronously = false;
container = NSPersistentCloudKitContainer(name: "swiftDataCloudTest", managedObjectModel: mom);
container.persistentStoreDescriptions = [description]

// 3. get modelContainer
let modelContainer = try! ModelContainer(for: Attachment.self, configurations: self.modelConfiguration)

this code works fine on MacOS(14.4.1) and previous iOS 17.x.(iOS 17.2 is OK)

but on iOS 17.5, the app crashes with message

A Core Data error occurred." UserInfo={NSLocalizedFailureReason=Unable to find a configuration named 'default' in the specified managed object model

how can I fix these?

Answered by DTS Engineer in 789842022

It seems to me that your code creates a persistent CloudKit container (NSPersistentCloudKitContainer) and a SwiftData model container (ModelContainer), and that both containers use the same model and manage the same store, which triggers a conflict. This is discussed in Avoid synchronizing a store with multiple persistent containers.

Assuming that you have correctly configured CloudKit in your project, when you create a SwiftData model container with the default configuration (ModelConfiguration), SwiftData creates a NSPersistentCloudKitContainer under the hood, which automatically synchronizes the data store for you, and so you don't really need to create the persistent CloudKit container.

Is there any special reason why you need a persistent CloudKit container and a SwiftData model container simultaneously?

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

the crash is on last line when create ModelContainer

on MacOS 14.5, the app on Mac also crash with the same error

Unresolved error loading container Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=Unable to find a configuration named 'default' in the specified managed object model.}

I think there is something todo with MacOS 14.5 and iOS 17.5.....

@FPST

I tried with catch

do {
  self.modelContainer = try ModelContainer(for: Document.self, CheckPoint.self, configurations: self.modelConfiguration)
} catch let error {
  print(error);
  fatalError()
}

I can't find any clues with the error message

CoreData: debug: CoreData+CloudKit: -[NSCloudKitMirroringDelegate managedObjectContextSaved:](3113): <NSCloudKitMirroringDelegate: 0x600002aec2d0>: Observed context save: <NSPersistentStoreCoordinator: 0x600003beab80> - <NSManagedObjectContext: 0x600002eea150>
CoreData: debug: CoreData+CloudKit: -[NSCloudKitMirroringDelegate remoteStoreDidChange:](3156): <NSCloudKitMirroringDelegate: 0x600002aec2d0>: Observed remote store notification: <NSPersistentStoreCoordinator: 0x600003beab80> - BBB83AF9-4C01-4B3C-83E2-995E86857622 - (null) - file:///Users/xx/Library/Containers/com.test.swiftDataCloudTest/Data/Library/Application%20Support/default.store
CoreData: debug: CoreData+CloudKit: -[NSCloudKitMirroringDelegate remoteStoreDidChange:]_block_invoke(3206): <NSCloudKitMirroringDelegate: 0x600002aec2d0> - Ignoring remote change notification because it didn't change any entities tracked by persistent history: <NSSQLCore: 0x11f60e6d0> (URL: file:///Users/xx/Library/Containers/com.test.swiftDataCloudTest/Data/Library/Application%20Support/default.store)
SwiftDataError(_error: SwiftData.SwiftDataError._Error.loadIssueModelContainer)

It seems to me that your code creates a persistent CloudKit container (NSPersistentCloudKitContainer) and a SwiftData model container (ModelContainer), and that both containers use the same model and manage the same store, which triggers a conflict. This is discussed in Avoid synchronizing a store with multiple persistent containers.

Assuming that you have correctly configured CloudKit in your project, when you create a SwiftData model container with the default configuration (ModelConfiguration), SwiftData creates a NSPersistentCloudKitContainer under the hood, which automatically synchronizes the data store for you, and so you don't really need to create the persistent CloudKit container.

Is there any special reason why you need a persistent CloudKit container and a SwiftData model container simultaneously?

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

swiftUI apps with SwiftData and CloudKit crashes on iOS but works on MacOS
 
 
Q