gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:56:28 24 4
gpt4 key购买 nike

我正在尝试映射一个名为 Product 的 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 的所有查询。

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

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

仍然是同样的错误

最佳答案

我尝试在新版本的 RestKit 中使用我自己的 CoreData 配置,但最终遇到了从不同上下文访问对象和数据不一致的几个问题。这是我的 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 sharedManager].managedObjectStore.mainQueueManagedObjectContext 访问生成上下文。
这应该允许您仍然与您的经理进行 CoreData 查询。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com