【发布时间】: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