gpt4 book ai didi

ios - 核心数据 - 如何处理第一次发布时的 _persistentStoreCoordinator 错误?

转载 作者:可可西里 更新时间:2023-11-01 06:08:51 25 4
gpt4 key购买 nike

我正在为首次发布的应用程序做最后的润色。我已经成功实现了核心数据,但不确定如何处理 persistentStoreCoordinator 方法,该方法显示“用代码替换此实现以适本地处理错误。”

如果我更改模型以进行更新,我将研究迁移,但现在我应该在这里做什么?

// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Data.sqlite"];

NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil 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;
}

最佳答案

NSPersistentStore 添加到 NSPersistentStoreCoordinator 失败是一个开发人员级错误。如果您已经进行了足够的测试,这在生产中应该永远不会失败。因此我总是把我自己的版本放在下面:

NSLog(@"Failed to load persistent store: %@\n%@", [error localizedDescription], [error userInfo]);
abort(); //My personal version throws a NSException

这是一个硬错误,在 99% 的情况下应该是一个硬错误。这绝对应该在开发中崩溃,这样您就可以知道它是否发生并且您被迫解决它。由于这是开发人员级别的错误,您可以保留此代码,因为它永远不会在生产中发生,如果发生,您希望将崩溃报告发送给 Apple。

关于ios - 核心数据 - 如何处理第一次发布时的 _persistentStoreCoordinator 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21323611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com