2014-12-08 72 views
0

,我發現了以下錯誤:RestKit: 'NSInternalInconsistencyException',理由是:「無法執行映射:無`managedObjectContext`分配

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to perform mapping: No `managedObjectContext` assigned. (Mapping response.URL = https://www.someurl.com/lastrequest=2014-12-08T02%3A44%3A52Z)' 

該應用程序在下面的行停在RKResponseMapperOperation.m:

- (RKMappingResult *)performMappingWithObject:(id)sourceObject error:(NSError **)error 
{ 

    NSLog(@"managedObjectContext: %@, Source Object: %@  Error: %@", self.managedObjectContext, sourceObject, (*error).description); 
    NSAssert(self.managedObjectContext, @"Unable to perform mapping: No `managedObjectContext` assigned. (Mapping response.URL = %@)", self.response.URL); 
.... 

我注意到上述方法在應用程序崩潰之前被稱爲27(這個數字變化)次。在每個實例中,的NSManagedObjectContext存在即低於線:

2014-12-07 18:44:48.721 MyApp[19011:3258405] managedObjectContext:managedObjectContext: <NSManagedObjectContext: 0x1701f5800>, Source Object: { 
    friends =  (
    ); 
}  Error: (null) 

然而右崩潰之前,所述的NSManagedObjectContext爲null:

2014-12-07 18:44:53.454 MyApp[19011:3258404] managedObjectContext: (null), Source Object: { 
    friends =  (
    ); 
}  Error: (null) 

由於該應用功能通常一會兒它崩潰之前,我我不知道如何解決這個問題。任何指針將不勝感激。

*編輯*

在Appdelegaate。這種方法在viewDidLoad中調用一次當

- (RKManagedObjectStore *)managedObjectStore 
{ 
    if (!_managedObjectStore && [Persistence loggedIn]) 
    { 
     NSError * error; 
     NSURL * modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"App" ofType:@"momd"]]; 
     NSManagedObjectModel * managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy]; 
     self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; 

     [_managedObjectStore createPersistentStoreCoordinator]; 

     NSArray * searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString * documentPath = [searchPaths objectAtIndex:0]; 

     NSString *dbName = [NSString stringWithFormat:@"%@App%@.sqlite", documentPath, [Persistence username]]; 
     NSPersistentStore * persistentStore = [_managedObjectStore addSQLitePersistentStoreAtPath:dbName 
                      fromSeedDatabaseAtPath:nil 
                       withConfiguration:nil 
                          options:[self optionsForSqliteStore] 
                          error:&error]; 
     NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error); 

     NSLog(@"Path: %@", dbName); 

     if(!persistentStore) 
     { 
      NSLog(@"Failed to add persistent store: %@", error); 
     } 

     [_managedObjectStore createManagedObjectContexts]; 
     self.managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:self.managedObjectStore.persistentStoreManagedObjectContext]; 

     return self.managedObjectStore; 
    } 

    return _managedObjectStore; 
} 

- (id)optionsForSqliteStore 
{ 
    return @{ 
      NSInferMappingModelAutomaticallyOption: @YES, 
      NSMigratePersistentStoresAutomaticallyOption: @YES 
      }; 
} 

用戶登錄創建MOC: 對於核心數據堆棧,我使用的AppDelegate默認核心數據代碼在Xcode中創建項目時商提供的。

- (NSManagedObjectContext *)managedObjectContext 
{ 
    if (_managedObjectContext != nil) { 
     return _managedObjectContext; 
    } 

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
    if (coordinator != nil) { 
     _managedObjectContext = [[NSManagedObjectContext alloc] init]; 
     [_managedObjectContext setPersistentStoreCoordinator:coordinator]; 
    } 
    return _managedObjectContext; 
} 

MOC操作:

- (void)saveContext 
{ 
    NSError *error = nil; 
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext; 
    if (managedObjectContext != nil) { 
     if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { 
      // Replace this implementation with code to handle the error appropriately. 
      // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } 
    } 
} 

裏面的應用程序,下面的方法來設置,獲取和明確的ObjectManager:

- (void)refreshMOC 
{ 
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    self.objectManager = [self getObjectManager]; 

    self.objectManager.managedObjectStore = appDelegate.managedObjectStore; 
    self.objectManager.managedObjectStore.managedObjectCache = appDelegate.managedObjectStore.managedObjectCache; 
    self.managedObjectContext = self.objectManager.managedObjectStore.mainQueueManagedObjectContext; 
} 

- (RKObjectManager *)setupObjectManager 
{ 
    NSURL *baseURL = [NSURL URLWithString:kBaseURL]; 
    AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:baseURL]; 
    RKObjectManager *manager = [[RKObjectManager alloc]initWithHTTPClient:httpClient]; 
    [manager.HTTPClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; 
    [manager setAcceptHeaderWithMIMEType:RKMIMETypeJSON]; 
    [manager.HTTPClient setParameterEncoding:AFJSONParameterEncoding]; 
    [RKMIMETypeSerialization registeredMIMETypes]; 
    [RKObjectManager setSharedManager:manager]; 

    return [RKObjectManager sharedManager]; 
} 

- (RKObjectManager *)getObjectManager 
{ 
    self.objectManager = (!self.objectManager) ? [self setupObjectManager] : self.objectManager; 
    return self.objectManager; 
} 

- (RKObjectManager*)newObjectManager 
{ 
    [self clearRKObjectManager]; 
    return [self getObjectManager]; 
} 

- (void)clearRKObjectManager 
{ 
    if (self.objectManager) 
    { 
     self.objectManager = nil; 
    } 
} 
+0

你在哪裏設置了核心數據堆棧並創建了MOC?顯示代碼。 – Wain 2014-12-08 09:17:58

+0

謝謝。請參閱上述編輯。 – user1107173 2014-12-08 15:08:00

+0

@Wain - 有什麼想法? – user1107173 2014-12-09 03:59:42

回答

1

刪除所有應用程序委託模板核心數據方法。當您使用RestKit並創建託管對象庫時,您要求RestKit爲您管理核心數據堆棧,因此不需要這些其他方法(並且混淆了事情)。

當您需要MOC時,從託管對象庫中獲取/一個。

請注意,上述內容也適用於保存,因爲您需要使用RestKit方法保存到持久性存儲區,而不僅僅是保存單個MOC。

+0

我確實刪除了所有的模板代碼,但錯誤仍然存​​在。我能想到的只有多線程。有時我可以有五個併發調度隊列取得。你認爲這可能是問題嗎?如果是這樣,有關如何使用RESTKit處理多線程的建議? – user1107173 2014-12-11 05:02:08

+1

你在用這些線程做什麼?只需觸發RestKit?怎麼樣? – Wain 2014-12-11 07:41:55

+1

它看起來像你的代碼中可能有一個洞,如果你曾經調用setupObjectManager而不調用refreshMoc – Wain 2014-12-11 07:45:20