6

我正在尝试查找有关在 iPhone 上创建持久存储协调器时处理错误的信息。我已经实现了轻量级迁移

   NSError *error = nil;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options 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;

这是基于 Apple 的代码,并增加了对轻量级迁移的支持。

如果应用程序在这里仍然会遇到错误,我找不到任何有关处理错误的信息。在我看来,如果无法创建数据库,则根本无法使用应用程序。

  • 我是否只是要求用户尝试重新安装应用程序并显示相关信息?
  • 我可以在添加有关错误的提示时保留 abort() 语句,否则会导致应用程序被 Apple 拒绝吗?
4

1 回答 1

16

Calling abort() in this situation is out of question. Any app that crashes will be rejected by Apple. And it does not solve the problem: Starting the app again will find the same store file and therefore fail again.

For the same reason, reinstalling the app does not help, and it would be a bad user experience.

Of course, the situation should not occur if the migration has been tested. But if this fatal error occurs and your app cannot open the database, you have to create a fresh database.

The exact steps to take depend on what is stored in the database and if/how you can recover the data. So you could

  • remove the old database file with [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil], or copy a default database file from your programs resources to storeURL,
  • call _persistentStoreCoordinator addPersistentStoreWithType:... again to open the new database.
  • perhaps fill the database again with data from a server, or whatever has to be done to recreate the data.
于 2012-12-10T19:22:40.313 回答