我的 iOS 中的 Core Data 有一些奇怪的问题,我似乎无法重现,它只是不时发生在一些报告它的用户身上。我从 iOS 崩溃报告中得到的错误:
核心数据:-[NSPersistentStoreCoordinator _coordinator_you_never_successfully_opened_the_database_so_saving_back_to_it_is_kinda_hard:] + 56
这是一个屏幕截图(省略了产品名称):
困难的是我没有得到任何关于该错误的搜索结果。这是我的(相关)代码:
保存:
-(void)save
{
if(!self.horecaMOC.hasChanges)return;
NSError *error;
[self.horecaMOC save:&error];
if(error)
{
NSLog(@"save error %@",error.localizedDescription);
}
}
商务部:
-(NSManagedObjectContext*)horecaMOC
{
if(!_horecaMOC)
{
NSPersistentStoreCoordinator *coordinator = self.horecaPSC;
if (coordinator != nil) {
_horecaMOC = [[NSManagedObjectContext alloc] init];
[_horecaMOC setPersistentStoreCoordinator:coordinator];
}
}
return _horecaMOC;
}
PSC:
-(NSPersistentStoreCoordinator*)horecaPSC
{
if(!_horecaPSC)
{
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"horeca.sqlite"];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
NSError *error = nil;
_horecaPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.horecaMOM];
if (![_horecaPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
return _horecaPSC;
}
妈妈:
-(NSManagedObjectModel*)horecaMOM
{
if(!_horecaMOM)
{
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"poi" withExtension:@"momd"];
_horecaMOM = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
}
return _horecaMOM;
}
这里的设置似乎没问题,因为 99% 的时间它都可以工作,但有时我会收到我没有打开数据库的错误。由于我无法调试,因此很难弄清楚是什么原因。PSC可能为零吗?那为什么会这样呢?另外,我知道 MOC 应该只绑定到 1 个线程,并且由于我无法让它崩溃,我认为这不会有问题吗?
感谢您的任何建议!