Я настроил DatabaseApplicationService, который
- Получает программно определенный NSManagedObjectModel
- Создает NSPersistentCloudKitContainer на основе этой модели с двумя конфигурациями: «Локальный» и «Облачный».
Все это работает нормально, как кажется.
lazy var persistentContainer: NSPersistentCloudKitContainer = {
// Ensure that we have the required path.
pathHandler.ensurePersistentStorePath()
#if DEBUG
// get the store description
guard let description = container.persistentStoreDescriptions.first
else {
fatalError("Could not retrieve a persistent store description.")
}
// initialize the CloudKit schema
try? container.initializeCloudKitSchema(options: NSPersistentCloudKitContainerSchemaInitializationOptions.dryRun)
#endif
let local = NSPersistentStoreDescription(url: pathHandler.persistentLocalStoreURL)
local.configuration = "Local"
let cloud = NSPersistentStoreDescription(url: pathHandler.persistentCloudStoreURL)
cloud.configuration = "Cloud"
cloud.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.com.innoreq.HappyFreelancer")
container.persistentStoreDescriptions = [ local, cloud ]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
В SceneDelegate
среда получает NSManagedObjectContext:
let contentView = ContentView().environment(\.managedObjectContext, databaseService.context)
В представлении В модели для контекста запрашивается контекст:
@Environment(\.managedObjectContext) var managedObjectContext: NSManagedObjectContext
Теперь при извлечении данных из контекста:
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "EWork")
do {
return try managedObjectContext.execute(fetchRequest) as! [IsTimedAndTagged]
} catch {
fatalError("Failed to fetch EWork")
}
эта ошибка выдается во время выполнения:
2020-01-23 16:34:05.981483+0100 XY[14121:20859222] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSPersistentStoreCoordinator for searching for entity name 'EWork''
При проверке координатора службы, это , а не ноль. Есть ли объяснение этому странному эффекту?