我花了很多时间让我的 Core Data 模型有效地为我的 iOS 应用程序开发工作。在大多数情况下,我的一切工作都很顺利,但前几天我在加载数据时遇到了一个独特的错误,这只是偶然。我刚开始在我的 iPhone 上运行我的应用程序进行测试,就在它启动并开始使用 Core Data 加载数据时,我姐姐打电话给我。时机相当偶然,因为该中断似乎abort()
已从persistentStoreCoordinator
.AppDelegate
这是我所指的代码:
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("DataModel.sqlite")
var failureReason = "There was an error creating or loading the application's saved data."
let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]
do {
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: mOptions)
} catch {
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error as NSError
let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() 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.
NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
abort() // THIS
}
return coordinator
}()
显然,我看到苹果在哪里评论说该abort()
功能只需要在开发期间使用,并且应该在生产中删除一次,但我想知道是否有人可以告诉我如何正确处理这种类型的错误?
我没有加载大量数据,我想在这种情况下我需要做的就是重新开始加载过程。
我想我真正要问的是,一旦发现错误,是否有简单的方法告诉它重试,还是我必须手动返回在应用程序启动时调用数据获取的方式?