我正在尝试映射一个Product
使用 Web 服务调用的 CoreData 实体。
NSString *objName = @"Product";
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL: baseUrl];
NSManagedObjectContext *context = [[MContextManager sharedContextManager] managedObjectContext];
NSManagedObjectModel *managedObjectModel = [[context persistentStoreCoordinator] managedObjectModel];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
[managedObjectStore createPersistentStoreCoordinator];
[managedObjectStore createManagedObjectContexts];
objectManager.managedObjectStore = managedObjectStore;
RKEntityMapping *objMap = [RKEntityMapping mappingForEntityForName:objName inManagedObjectStore:managedObjectStore];
[objMap addAttributeMappingsFromDictionary: @{
@"CODE": @"code",
@"LABEL": @"label",
}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objMap pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
[objectManager getObjectsAtPath:objName parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"ok");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"ko");
}];
return;
你会猜到MContextManager
是我的上下文管理器,我用它来处理我对 CoreData 的所有查询。
我试图映射这个简单的实体已经有几个小时了,我得到的错误如下:
无法创建托管对象上下文:持久存储协调器没有任何持久存储。这可能意味着您忘记添加持久存储,或者您尝试这样做失败并出现错误。
我虽然在第一行设置了持久存储,不是吗?
更新
我已经为持久存储添加了这个:
NSURL *appDocs = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSString *storeURL = [appDocs URLByAppendingPathComponent:@"my-model.sqlite"].absoluteString;
[managedObjectStore addSQLitePersistentStoreAtPath:storeURL fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:nil];
仍然有同样的错误