RestKit核心数据&managedObjectStore是零'

时间:2014-04-06 19:11:23

标签: ios core-data restkit restkit-0.20

我不知道这是否是我问题的根本原因,但在使用

提出请求时
appropriateObjectRequestOperationWithObject:nil method:RKRequestMethodGET path:path parameters:nil

它做了一些工作,当试图映射响应时给了我这个警告:

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

接下来是:

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

我认为这是因为它不符合我对托管对象映射的请求,但我无法弄清楚原因。我正在使用此代码创建持久性存储:

// Initialize managed object store
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
NSError *error = nil;
[managedObjectStore createPersistentStoreCoordinator];
BOOL success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error);
if (! success) {
    RKLogError(@"Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error);
}
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Store.sqlite"];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
if (! persistentStore) {
    RKLogError(@"Failed adding persistent store at path '%@': %@", path, error);
}
[managedObjectStore createManagedObjectContexts];

适当的映射/响应描述符:

RKEntityMapping *containerMapping = [RKEntityMapping mappingForEntityForName:@"Container" inManagedObjectStore:managedObjectStore];
[containerMapping addAttributeMappingsFromDictionary:@{
                                                  @"id" : @"containerId",
                                                  @"name" : @"name",
                                                  @"public" : @"isPublic",
                                                  @"user": @"userId",
                                                  }];
containerMapping.identificationAttributes = @[@"containerId"];

responseDescriptor = [RKResponseDescriptor 
    responseDescriptorWithMapping:containerMapping
    method:RKRequestMethodAny
    pathPattern:nil 
    keyPath:@"containers"
    statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

1 个答案:

答案 0 :(得分:4)

您似乎尚未使用对托管对象库的引用配置对象管理器。这应该在您创建对象管理器时完成:

objectManager.managedObjectStore = managedObjectStore;

没有这个RestKit会回归到使用普通对象操作。

注意:如果您重新记录警告,则会在日志输出中看到Asked to create an RKManagedObjectRequestOperation object, but managedObjectStore is nil