3
我試圖將使用Restkit 0.1的現有應用程序更新爲0.20,但在設置核心數據堆棧時遇到了一些問題,因爲它運行到異常中。使用RestKit的核心數據初始化0.20
我有下面的代碼在我的AppDelegate:
/*
Complete Core Data stack initialization
*/
NSError *error = nil;
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"AppModel" ofType:@"momd"]];
// NOTE: Due to an iOS 5 bug, the managed object model returned is immutable.
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"TSI.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"];
//NSPersistentStore __unused *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
/*
RESTKIT
*/
// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
然後我映射正在TableViewController中設置按:
RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];
NSManagedObjectContext* context = [managedObjectStore mainQueueManagedObjectContext];
NSString *rootKeyPath;
rootKeyPath = @"body";
NSString *endpoint = @"account/login.json";
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
[entityMapping addAttributeMappingsFromDictionary:@{
@"api_token":@"token",
@"email": @"email",
@"first_name": @"name",
@"last_name": @"surname"
}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping pathPattern:endpoint keyPath:rootKeyPath statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[TSI_SERVER stringByAppendingString:endpoint]]];
RKManagedObjectRequestOperation *managedObjectRequestOperation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
//managedObjectRequestOperation.managedObjectContext = self.managedObjectContext;
[[NSOperationQueue currentQueue] addOperation:managedObjectRequestOperation];
但每當我試着檢索數據在視圖加載後,我在控制檯中出現以下錯誤(出於安全原因,我已屏蔽了我的應用使用的名稱和服務器):
2013-04-16 14:41:50.576 <<MyApp>>[4525:907] I restkit:RKLog.m:34 RestKit logging initialized...
2013-04-16 14:41:51.009 <<MyApp>>[4525:907] [HEATMA.PS] Running Heatma.ps version 4.0.2. For help go to www.heatma.ps/support
2013-04-16 14:42:54.464 <<MyApp>>[4525:3907] *** Assertion failure in
-[RKManagedObjectResponseMapperOperation performMappingWithObject:error:], /Users/manuel_matute/Code/RestKit/Code/Network/RKResponseMapperOperation.m:358
2013-04-16 14:42:59.876 <<MyApp>>[4525:3907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to perform mapping: No `managedObjectContext` assigned. (Mapping response.URL = http://www.<<myserver>>.com/rest/account/login.json)'
*** First throw call stack: (0x32a7b2a3 0x3a7a397f 0x32a7b15d 0x33350ab7 0xe37e1 0xe1b97 0x333085c1 0x33380be3 0x3abbb11f 0x3abbf961 0x3abbfac1 0x3abefa11 0x3abef8a4) libc++abi.dylib: terminate called throwing an exception
有沒有人有關於可能是什麼問題的想法?
謝謝!