繁体   English   中英

RestKit核心数据“ managedObjectStore为零”

[英]RestKit Core Data 'managedObjectStore is nil'

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

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在日志输出中Asked to create an RKManagedObjectRequestOperation object, but managedObjectStore is nil

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM