gpt4 book ai didi

ios - CoreData 很少 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'

转载 作者:行者123 更新时间:2023-12-02 02:36:06 30 4
gpt4 key购买 nike

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

NSInternalInconsistencyException(SIGABRT) This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.

    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

this is my Code:

- (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
{
}
}
}];
}

最佳答案

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

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

因此,我建议如下:

  1. 删除 @try/@catch block ,它们不会执行任何操作
  2. 如果不会导致应用崩溃,请将您的 DLog 调用至少更改为 ALog 调用,并提供有关商店拒绝添加原因的信息。

关于ios - CoreData 很少 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34195795/

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