我在“尝试保存()”此行遭遇崩溃。
`$cookies.getObject('myFavoritec');
}
在appdelegte中,appDidFinishLaunching:我写过:
extension NSManagedObjectContext {
/// Only save if `hasChanges`
func saveIfNeeded() throws {
guard hasChanges else {
return
}
do {
try save()
} catch {
assertionFailure("Failed to save main context: \(error)")
throw error
}
}
/// Calls `saveIfNeeded` and then calls `saveIfNeededUpToRootContext()` on the `parentContext`.
func saveIfNeededUpToRootContext() throws {
try saveIfNeeded()
try parent?.saveIfNeededUpToRootContext()
}
基本上调用上面的方法:saveIfNeededUpToRootContext。
以下是DataStore的初始化:
defer {
do {
try DataStore.mainContext.saveIfNeededUpToRootContext()
} catch {
log("Failed to save \"App Feedback\" to Core Data: \(error)")
}
}