2015-12-10 63 views
0

此NSPersistentStoreCoordinator沒有持久性存儲。它不能執行保存操作。CoreData很少'此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 
      { 
      } 
     } 
    }]; 
} 

回答

0

首先,@try/@catch將永遠不會觸發。核心數據不會像Objective-C中那樣引發異常。

其次,好像您的addPersistentStoreWithType...失敗,但您的DLog(假設它基於我的DLog)在生產中保持沉默。

因此,我建議如下:

  1. 取出@try/@catch塊,他們沒有做任何事情
  2. 更改DLog調用至少ALog電話如果不全力以赴與崩潰的應用程序,而不是有關商店爲什麼拒絕添加的信息。
+0

感謝,崩潰發生很少,我很好奇爲什麼商店拒絕添加... – fory

+0

有了這些DLog語句,你永遠不會看到它。你需要改變它們並等待錯誤再次發生。 –

相關問題