CoreData很少'这个NSPersistentStoreCoordinator没有持久存储。它无法执行保存操作。'

时间:2015-12-10 07:14:26

标签: ios core-data crash

此NSPersistentStoreCoordinator没有持久存储。它无法执行保存操作。

  

NSInternalInconsistencyException(SIGABRT)   此NSPersistentStoreCoordinator没有持久存储。它无法执行保存操作。

    0   CoreFoundation  0x000000018268a59c  ___exceptionPreprocess + 132
    1   libobjc.A.dylib 0x0000000192dd40e4  objc_exception_throw + 56
    2   CoreData    0x000000018240a658  ___65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 5080
    3   CoreData    0x0000000182411654  _gutsOfBlockToNSPersistentStoreCoordinatorPerform + 180
    4   libdispatch.dylib   0x000000019341936c  __dispatch_client_callout + 16
    5   libdispatch.dylib   0x00000001934226e8  __dispatch_barrier_sync_f_invoke + 76
    6   CoreData    0x0000000182404cb4  __perform + 180
    7   CoreData    0x0000000182342c34  -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 300
    8   CoreData    0x0000000182342c64  -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 348
    9   CoreData    0x0000000182369400  -[NSManagedObjectContext save:] + 1284
    10  RBookReader 0x00000001000109d4  __44-[RCCoreDataManager mocDidSaveNotification:]_block_invoke (RCCoreDataManager.m:186)
    11  CoreData    0x00000001823dd270  _developerSubmittedBlockToNSManagedObjectContextPerform + 200
    4   libdispatch.dylib   0x000000019341936c  __dispatch_client_callout + 16
    13  libdispatch.dylib   0x00000001934234c0  __dispatch_queue_drain + 1216
    14  libdispatch.dylib   0x000000019341c474  __dispatch_queue_invoke + 132
    15  libdispatch.dylib   0x0000000193425224  __dispatch_root_queue_drain + 664
    16  libdispatch.dylib   0x000000019342675c  __dispatch_worker_thread3 + 108
    17  libsystem_pthread.dylib 0x00000001935f52e4  _pthread_wqthread + 812
    18  libsystem_pthread.dylib 0x00000001935f4fa8  __pthread_set_self + 12

这是我的代码:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (!_persistentStoreCoordinator)
    {
        self.storeURL =[NSURL fileURLWithPath:[XQDocumentPath() stringByAppendingPathComponent:@"Model.sqlite"] isDirectory:NO];

        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                       [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                       [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                       nil];

        NSError *error = nil;
        _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
        @try {
            if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:self.storeURL options:options error:&error]) {
                if (error) {
                    DLog(@"error: %@", error.localizedDescription);
                    DLog(@"rm \"%@\"", self.storeURL.path);
                }
            };
        }
        @catch (NSException *exception) {
            DLog(@"addPersistentSoreWithType fail, reason = %@",exception.description);
        }
        @finally {

        }
    }

    return _persistentStoreCoordinator;
}


#pragma mark
#pragma mark -context save notification
- (void)mocDidSaveNotification:(NSNotification *)noti
{
    NSManagedObjectContext *savedContext = [noti object];

    // Ignore change notifications for the top MOC.
    if (!savedContext.parentContext) {
        return;
    }

    if (!savedContext.persistentStoreCoordinator) {
        return;
    }

    // Ignore changes for other databases.
    if (self.privateObjectContext.persistentStoreCoordinator != savedContext.persistentStoreCoordinator) {
        return;
    }

    [savedContext.parentContext performBlock: ^{
        NSError *error;
        if (savedContext.parentContext.hasChanges) {
            @try
            {
                if (![savedContext.parentContext save: &error]) {
                    NSLog(@"Error saving context %@: %@", savedContext.parentContext, [error localizedDescription]);
#if defined DEBUG && defined TEST
                    [self showValidationError:error];
#else
#endif
                }
            }
            @catch(NSException *exception)
            {
                DLog(@"Unable to perform save: %@", (id)[exception userInfo] ?: (id)[exception reason]);
            }
            @finally
            {
            }
        }
    }];
}

1 个答案:

答案 0 :(得分:0)

首先,@try/@catch永远不会开火。 Core Data不会像Objective-C那样抛出异常。

其次,您的addPersistentStoreWithType...似乎失败了,但您的DLog(假设它基于我的DLog)在生产中是静默的。

因此,我建议如下:

  1. 移除@try/@catch块,他们什么都不做
  2. 将您的DLog来电更改为至少ALog次来电,如果不是因为应用程序崩溃而导致商店拒绝添加的原因信息。