,我發現了以下錯誤: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;
}
}
你在哪裏設置了核心數據堆棧並創建了MOC?顯示代碼。 – Wain 2014-12-08 09:17:58
謝謝。請參閱上述編輯。 – user1107173 2014-12-08 15:08:00
@Wain - 有什麼想法? – user1107173 2014-12-09 03:59:42