ios - RestKit核心数据“managedObjectStore为零”

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

最佳答案

似乎您没有使用对托管对象存储的引用来配置对象管理器。创建对象管理器时,应该这样做:

objectManager.managedObjectStore = managedObjectStore;

没有这个RestKit就会退回到使用普通对象操作来处理所有事情。

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

关于ios - RestKit核心数据“managedObjectStore为零”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22898653/

相关文章:

ios - 如何为核心数据持久性配置RestKit?

iphone - 如何获取关系键

android - 弹跳按钮 flutter

ios - 神奇的记录将大量数据保存到数据库

ios - NSFetchedResultsController 与项目在多个部分?

ios - CoreData 无法生成 getter 和 setter

ios - 用于与 RestKit 0.20.1 进行 CoreData 映射的网络连接委托(delegate)?

ios - 以编程方式获取IOS设备的UDID?

ios - UICollectionViewFlowLayout 的行为未定义

ios - 在 Xcode 中查找未使用的文件