首先,我试图在不使用核心数据的情况下实现restKit。一切正常。但现在我想在其中实现核心数据。我创建了一个单独的类,我将所有核心数据和映射内容放在一起。
我在其中创建了一个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映射到核心数据实体。我在这个方法中唯一改变的是来自
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".'
修改 好吧,我在初始化我的RKObjectManager后推出了managedObjectstore。
objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
objectManager.managedObjectStore = managedObjectStore;
但现在我收到了以下错误。
** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: self.managedObjectContext'
答案 0 :(得分: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];