我只是为我的应用程序完成第一次发布的最后润色。我已成功实现了Core Data,但不确定如何处理persistentStoreCoordinator
方法,该方法将“用代码替换此实现以正确处理错误。”
如果我更改模型以进行更新,我会调查迁移,但现在我该怎么办?
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Data.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
/*
Replace this implementation 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.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
答案 0 :(得分:1)
NSPersistentStore
添加到NSPersistentStoreCoordinator
的失败是开发者级别错误。如果你做了足够的测试,这应该永远不会失败。因此,我总是把自己的版本放在下面:
NSLog(@"Failed to load persistent store: %@\n%@", [error localizedDescription], [error userInfo]);
abort(); //My personal version throws a NSException
这是一个很难的错误,99%的时间都应该是一个很难的错误。这绝对应该在开发中崩溃,以便您可以知道它是否发生并且您被迫解决它。由于这是一个开发人员级别的错误,您可以保留此代码,因为它永远不会在生产中发生,如果确实如此,您需要将崩溃报告发送给Apple。