【问题标题】:RestKit Core Data 'managedObjectStore is nil'RestKit 核心数据'managedObjectStore 为零'
【发布时间】:2014-04-06 19:11:23
【问题描述】:

我不知道这是否是我的问题的根本原因,但是当我提出请求时使用

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)];

【问题讨论】:

  • 您的响应描述符应该有一个路径模式集。你从appropriateObjectRequestOperationWithObject 得到什么类型的课程?
  • 我侧载了很多项目,因此它可能来自许多不同的路径(例如,来自 /containers,或者来自 /users,因为侧载了他们的容器列表)。
  • 您是否加载了响应描述符无效的任何路径?您是否给对象管理器提供了对托管对象存储的引用?
  • "您是否给对象管理器提供了对托管对象存储的引用?" - 这也是我的第一个想法,因为我找到的示例都没有显示如何做到这一点。我想知道它如何知道对象存储在哪里。我该怎么做?
  • (facepalm) - objectManager.managedObjectStore = managedObjectStore;

标签: ios core-data restkit restkit-0.20


【解决方案1】:

您似乎没有为对象管理器配置对托管对象存储的引用。这应该在您创建对象管理器时完成:

objectManager.managedObjectStore = managedObjectStore;

没有这个 RestKit 就只能使用普通的对象操作来处理所有事情。

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

【讨论】:

    猜你喜欢
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    相关资源
    最近更新 更多