处理addPersistentStoreWithType中的错误 [英] Handling errors in addPersistentStoreWithType

查看:723
本文介绍了处理addPersistentStoreWithType中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to find information on handling errors when creating a persistent store coordinator on the iPhone. I have implemented lightweight migration

   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;

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

This is based on the code from Apple with the added support for lightweight migration.

如果应用程式在这里仍然遇到错误,我找不到任何处理错误的资讯。在我看来,如果数据库无法创建应用程序不能使用。

I can't find any information on handling errors if the application would still encounter an error here. It seems to me that if the database cannot be created the application can't be used at all.


  • 我只是要求用户尝试重新安装应用程序并显示相关信息?

  • 我可以在添加关于错误的提示时保留abort()语句,否则会导致Apple拒绝该应用程序?

推荐答案

在这种情况下调用abort任何崩溃的应用程序都会被Apple拒绝。并且它不解决问题:再次启动应用程序将找到相同的存储文件,因此再次失败。

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.

具体步骤取决于数据库中存储的内容以及如何/如何恢复数据。所以你可以用 [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

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

    / code>,或将默认数据库文件从您的程序资源复制到 storeURL
  • 调用 _persistentStoreCoordinator addPersistentStoreWithType:再次打开新数据库。

  • 可能再次使用来自服务器的数据填充数据库,重新创建数据。

  • 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.

这篇关于处理addPersistentStoreWithType中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆