container.loadPersistentStores 后 NSPersistentStoreCoordinator 未初始化
NSPersistentStoreCoordinator not initializing after container.loadPersistentStores
我正在尝试使用 NSPersistentCloudKitContainer
在 WatchOS 应用程序上设置 CoreData/CloudKit。但是,在调用 container.loadPersistentStores
之后,viewContext.persistentStoreCoordinator
字段似乎仍然为零。
以下是一些代码示例:
Persistence.swift
struct PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentCloudKitContainer
init() {
container = NSPersistentCloudKitContainer(name: "Test")
guard let description = container.persistentStoreDescriptions.first else {
fatalError("Could not retrieve a persistent store description.")
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
}
}
MainApp.swift
@main
struct EventLoggerApp: App {
let persistenceController = PersistenceController.shared
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
}
}
}
ContentView.swift
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
func createRecord() {
print(viewContext.persistentStoreCoordinator) // this is nil
let sensorReadingDescription = NSEntityDescription.entity(forEntityName: "SensorReading", in: viewContext)! // this crashes
}
}
我崩溃的错误是:
2020-12-12 11:56:32.205433-0800 EventLoggerWatch WatchKit Extension[400:541990] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSPersistentStoreCoordinator for searching for entity name 'Test''
*** First throw call stack:
(0x1f4bb7dc 0x1edc9c04 0x23d68a44 0x174d34 0x174378 0x22fa9d6c 0x231422e0 0x23256ebc 0x22cb86a8 0x23256ed8 0x23256ebc 0x2327a6a4 0x2330f2dc 0x2324cdac 0x2324b6f4 0x2324b75c 0x2324bd20 0x4076df2c 0x40bd9f44 0x40763d70 0x40763e80 0x40763ce8 0x40b986cc 0x40b75abc 0x40becf04 0x40be59b4 0x1f43c4ec 0x1f43c3ec 0x1f43b74c 0x1f435dac 0x1f435574 0x235f94c0 0x40b580f8 0x40b5d5f8 0x350d9b68 0x1eed893c)
libc++abi.dylib: terminating with uncaught exception of type NSException
如果我能提供更多有用的上下文,请告诉我,在此先感谢您的指导!
经过进一步调查,我不确定这个问题是否有效。我认为上面列出的代码只是 运行 用于 phone 目标,而不是手表。手表也可能存在问题,但我会先让 phone 工作,因为问题可能就在那里。感谢所有在这里提供帮助的人!
我正在尝试使用 NSPersistentCloudKitContainer
在 WatchOS 应用程序上设置 CoreData/CloudKit。但是,在调用 container.loadPersistentStores
之后,viewContext.persistentStoreCoordinator
字段似乎仍然为零。
以下是一些代码示例:
Persistence.swift
struct PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentCloudKitContainer
init() {
container = NSPersistentCloudKitContainer(name: "Test")
guard let description = container.persistentStoreDescriptions.first else {
fatalError("Could not retrieve a persistent store description.")
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
}
}
MainApp.swift
@main
struct EventLoggerApp: App {
let persistenceController = PersistenceController.shared
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
}
}
}
ContentView.swift
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
func createRecord() {
print(viewContext.persistentStoreCoordinator) // this is nil
let sensorReadingDescription = NSEntityDescription.entity(forEntityName: "SensorReading", in: viewContext)! // this crashes
}
}
我崩溃的错误是:
2020-12-12 11:56:32.205433-0800 EventLoggerWatch WatchKit Extension[400:541990] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSPersistentStoreCoordinator for searching for entity name 'Test''
*** First throw call stack:
(0x1f4bb7dc 0x1edc9c04 0x23d68a44 0x174d34 0x174378 0x22fa9d6c 0x231422e0 0x23256ebc 0x22cb86a8 0x23256ed8 0x23256ebc 0x2327a6a4 0x2330f2dc 0x2324cdac 0x2324b6f4 0x2324b75c 0x2324bd20 0x4076df2c 0x40bd9f44 0x40763d70 0x40763e80 0x40763ce8 0x40b986cc 0x40b75abc 0x40becf04 0x40be59b4 0x1f43c4ec 0x1f43c3ec 0x1f43b74c 0x1f435dac 0x1f435574 0x235f94c0 0x40b580f8 0x40b5d5f8 0x350d9b68 0x1eed893c)
libc++abi.dylib: terminating with uncaught exception of type NSException
如果我能提供更多有用的上下文,请告诉我,在此先感谢您的指导!
经过进一步调查,我不确定这个问题是否有效。我认为上面列出的代码只是 运行 用于 phone 目标,而不是手表。手表也可能存在问题,但我会先让 phone 工作,因为问题可能就在那里。感谢所有在这里提供帮助的人!