objective-c – 处理addPersistentStoreWithType中的错误

objective-c – 处理addPersistentStoreWithType中的错误,第1张

概述我试图在iPhone上创建持久存储协调器时查找有关处理错误的信息.我已经实现了轻量级迁移 NSError *error = nil;NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigrate 我试图在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:…以打开新数据库.>或许用服务器中的数据再次填充数据库,或者重新创建数据所需的任何 *** 作.

总结

以上是内存溢出为你收集整理的objective-c – 处理addPersistentStoreWithType中的错误全部内容,希望文章能够帮你解决objective-c – 处理addPersistentStoreWithType中的错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1234524.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存