objective-c – 处理addPersistentStoreWithType中的错误

前端之家收集整理的这篇文章主要介绍了objective-c – 处理addPersistentStoreWithType中的错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在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拒绝?

解决方法

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

出于同样的原因,重新安装应用程序没有帮助,这将是一个糟糕的用户体验.

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

确切的步骤取决于数据库中存储的内容以及是否/如何恢复数据.所以你可以

>使用[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]删除数据库文件,或将默认数据库文件从程序资源复制到storeURL,>再次调用_persistentStoreCoordinator addPersistentStoreWithType:…以打开新数据库.>或许用服务器中的数据再次填充数据库,或者重新创建数据所需的任何操作.

猜你在找的C&C++相关文章