【问题标题】:CoreData rarely 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'CoreData 很少 '这个 NSPersistentStoreCoordinator 没有持久存储。它无法执行保存操作。
【发布时间】:2016-03-15 17:29:22
【问题描述】:

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

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

0 核心基础 0x000000018268a59c ___exceptionPreprocess + 132 1 libobjc.A.dylib 0x0000000192dd40e4 objc_exception_throw + 56 2 CoreData 0x000000018240a658 ___65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 5080 3 核心数据 0x0000000182411654 _gutsOfBlockToNSPersistentStoreCoordinatorPerform + 180 4 libdispatch.dylib 0x000000019341936c __dispatch_client_callout + 16 5 libdispatch.dylib 0x00000001934226e8 __dispatch_barrier_sync_f_invoke + 76 6 核心数据 0x0000000182404cb4 __perform + 180 7 核心数据 0x0000000182342c34-[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 300 8 核心数据 0x0000000182342c64 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 348 9 核心数据 0x0000000182369400 -[NSManagedObjectContext 保存:] + 1284 10 RBookReader 0x00000001000109d4 __44-[RCCoreDataManager mocDidSaveNotification:]_block_invoke (RCCoreDataManager.m:186) 11 核心数据 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
            {
            }
        }
    }];
}

【问题讨论】:

    标签: ios core-data crash


    【解决方案1】:

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

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

    因此,我建议如下:

    1. 删除 @try/@catch 块,它们什么都不做
    2. 将您的 DLog 调用更改为至少 ALog 调用,如果不是完全崩溃应用程序,而是提供有关商店拒绝添加的原因的信息。

    【讨论】:

    • 使用这些 DLog 语句,您将永远看不到它。您需要更改这些并等待错误再次发生。
    猜你喜欢
    • 2020-05-30
    • 2012-08-18
    • 2013-03-07
    • 1970-01-01
    • 2016-02-04
    • 2019-11-12
    • 1970-01-01
    • 2014-10-15
    • 2013-04-08
    相关资源
    最近更新 更多