objective-c - RestKit - 无法映射实体

标签 objective-c restkit-0.20

我正在尝试将名为 Product 的 CoreData 实体与 Web 服务映射。

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 的所有查询。

我已经尝试映射这个简单的实体几个小时了,我收到的错误如下:

Cannot create managed object contexts: The persistent store coordinator does not have any persistent stores. This likely means that you forgot to add a persistent store or your attempt to do so failed with an error.

我虽然在第一行设置了持久存储,不是吗?

更新

我已经为持久存储添加了这个:

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

还是同样的错误

最佳答案

我尝试将自己的 CoreData 配置与新版本的 RestKit 一起使用,最终遇到了从不同上下文访问对象和数据不一致的多个问题。这是我的 RestKit 初始化的代码:

NSError * error;
NSURL * modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"momd"]];
NSManagedObjectModel * managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore * managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

[managedObjectStore createPersistentStoreCoordinator];

NSArray * searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentPath = [searchPaths objectAtIndex:0];
NSPersistentStore * persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:[NSString stringWithFormat:@"%@/CoreData.sqlite", documentPath] fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];

if(!persistentStore){
  NSLog(@"Failed to add persistent store: %@", error);
}

[managedObjectStore createManagedObjectContexts];

[RKManagedObjectStore setDefaultStore:managedObjectStore];

_objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:3000/"]];
_objectManager.managedObjectStore = managedObjectStore;

// Object Mapping Code...

从上下文管理器中,您可以使用[RKObjectManager共享管理器].managedObjectStore.mainQueueManagedObjectContext访问生成上下文 这应该允许您仍然向您的经理进行 CoreData 查询。

关于objective-c - RestKit - 无法映射实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16695046/

相关文章:

ios - UILabel 中的动画文本更改

objective-c - 带有月份名称的日期 Objective-C

ios - Restkit 无法将请求序列化为 xml

ios - RestKit 0.2,多种方式发起GET、POST、PUT请求

ios - 如何使用 OCMock stub 方法与响应 block

iphone - 如何访问项目中的本地文件

ios - 从 NSMutableArray 创建一个 NSMUtableDictionary

ios - 错误: "The sandbox is not in sync with the Podfile.lock..." after installing RestKit with cocoapods

restkit - 在 RestKit 中,RKRoute 获取对象列表以及单个对象的最佳方法是什么?