iOS核心数据NSPersistentStoreCoordinator从未打开过数据库

Peg*_*a88 5 xcode core-data objective-c ios nspersistentstore

我的iOS中的核心数据有一些奇怪的问题,我似乎无法重现,它只是偶尔会发生一些报告它的用户.我从iOS崩溃报告中得到的错误:

CoreData: - [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);
    }
}
Run Code Online (Sandbox Code Playgroud)

商务部:

-(NSManagedObjectContext*)horecaMOC
{
    if(!_horecaMOC)
    {
        NSPersistentStoreCoordinator *coordinator = self.horecaPSC;
        if (coordinator != nil) {
            _horecaMOC = [[NSManagedObjectContext alloc] init];
            [_horecaMOC setPersistentStoreCoordinator:coordinator];
        }
    }
    return _horecaMOC;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

妈妈:

-(NSManagedObjectModel*)horecaMOM
{
    if(!_horecaMOM)
    {
        NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"poi" withExtension:@"momd"];
        _horecaMOM = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    }
    return _horecaMOM;
}
Run Code Online (Sandbox Code Playgroud)

看起来设置在这里是正常的,因为99%的时间它工作,但有时我得到错误,我没有打开数据库.由于我无法调试,因此很难弄清楚原因是什么.PSC可能没有?那为什么呢?另外,我知道MOC应该只绑定1个线程,因为我不能让它崩溃我不认为这可能有问题吗?

谢谢你的建议!