【问题标题】:Handling errors in addPersistentStoreWithType处理 addPersistentStoreWithType 中的错误
【发布时间】:2012-11-28 05:08:58
【问题描述】:

我正在尝试查找有关在 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 拒绝吗?

【问题讨论】:

    标签: objective-c core-data fatal-error


    【解决方案1】:

    在这种情况下调用 abort() 是没有问题的。任何崩溃的应用程序都将被 Apple 拒绝。而且它并没有解决问题:再次启动应用会发现相同的商店文件,因此再次失败。

    出于同样的原因,重新安装应用程序也无济于事,而且会带来糟糕的用户体验。

    当然,如果迁移已经过测试,则不会出现这种情况。但是,如果发生此致命错误并且您的应用无法打开数据库,则您必须创建一个新的数据库。

    具体采取的步骤取决于数据库中存储的内容以及是否/如何恢复数据。所以你可以

    • 使用[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] 删除旧数据库文件,或将默认数据库文件从程序资源复制到storeURL
    • 再次拨打_persistentStoreCoordinator addPersistentStoreWithType:...打开新数据库。
    • 可能会再次使用来自服务器的数据填充数据库,或者重新创建数据必须执行的任何操作。

    【讨论】:

    • 感谢您的帮助我已添加尝试删除数据库然后再次创建它,因为无法从服务器加载数据库。如果此时仍然失败,我该怎么办?
    • 这将是一个不应该正常发生的致命错误,所以我认为在这种情况下,abort() 是可以的,这样你就会得到通知的崩溃日志你的问题。
    • 如果您继续尝试使用 addpersistentstorewithtype 加载数据库并失败,您会不会陷入无限循环?或类似的东西?
    • 另外,我可以以某种方式强制 addPersistentStoreWithType: 失败吗?
    猜你喜欢
    • 2012-01-04
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 2017-10-26
    相关资源
    最近更新 更多