2013-01-09 41 views
1

首先,我試圖實現restKit而不使用核心數據。一切正常。但現在我想在其中實現核心數據。我做了一個單獨的課,我把我所有的核心數據的東西和映射的東西英寸RestKit RKManagedObjectRequestOperation預計

我做了一個init函數,它看起來像這樣。

-(API *)init{ 

self = [super init]; 

if (self != nil){ 

    NSURL *baseURL = [NSURL URLWithString:@"http://virtuele-receptie.preview.sanmax.be"]; 

    AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; 

    [client setDefaultHeader:@"Accept" value:RKMIMETypeJSON]; 

    objectManager = [RKObjectManager managerWithBaseURL:baseURL]; 

    _managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; 

    managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:_managedObjectModel]; 

    objectManager.managedObjectStore = managedObjectStore; 

} 

return self; 

}

然後,我有一個功能mapLogin,這是要去的json映射到核心數據entities.The只有我在這個方法中改變的事情是從

RKObjectMapping *dataMapping = [RKObjectMapping mappingForClass:[Data class]]; 

RKEntityMapping* dataMapping = [RKEntityMapping mappingForEntityForName:@"Data" inManagedObjectStore:managedObjectStore]; 

現在,當我運行我得到一個巨大的日誌。但最重要的事情就是我想到的。

LOG

W restkit:RKObjectManager.m:488 Asked to create an `RKManagedObjectRequestOperation` object, but managedObjectStore is nil. 

CoreData: error: Failed to call designated initializer on NSManagedObject class 'Data' 

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Data 0x1fd0b390> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "status".' 

編輯 奧凱,我的推杆在managedObjectstore後,我intialized我RKObjectManager。

objectManager = [[RKObjectManager alloc] initWithHTTPClient:client]; 
     objectManager.managedObjectStore = managedObjectStore; 

但現在我得到了以下錯誤。

** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: self.managedObjectContext' 

回答

0

創建管理對象存儲後做這樣的事情:

[managedObjectStore createPersistentStoreCoordinator]; 
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"RKTwitter.sqlite"]; 
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"]; 
NSError *error; 
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error]; 
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error); 

// Create the managed object contexts 
[managedObjectStore createManagedObjectContexts];