1

首先,我尝试在不使用核心数据的情况下实现 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];

现在,当我跑步时,我会得到一个巨大的日志。但我认为最重要的是这些。

日志

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'
4

1 回答 1

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];
于 2013-01-17T16:52:09.477 回答