无法让 Restkit 0.20 + CoreData 工作
Posted
技术标签:
【中文标题】无法让 Restkit 0.20 + CoreData 工作【英文标题】:Can't make Restkit 0.20 + CoreData work 【发布时间】:2013-02-21 19:03:43 【问题描述】:我正在尝试从 http json 资源(类别列表)中获取内容并将其与 restkit 和 coredata 连接。
当我不使用 CoreData 时,我的映射曾经可以工作。然后我决定使用以下教程:
http://www.alexedge.co.uk/portfolio/introduction-restkit-0-20/
但是,我遇到了一个奇怪的错误,我无法找出原因:
the entity (null) is not key value coding-compliant for the key "remoteId"
我的类别模型/实体已将 remoteId 映射到我的服务器上的 id,所以这不是问题。但是,从错误看来,Restkit 或 CoreData 无法弄清楚我在说哪个实体(他们说它是一个空实体??)
这是请求代码:
- (NSFetchedResultsController *)fetchedResultsController
if (!_fetchedResultsController)
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Category class])];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext sectionNameKeyPath:nil cacheName:@"Category"];
self.fetchedResultsController.delegate = self;
NSError *error;
[self.fetchedResultsController performFetch:&error];
NSLog(@"%@",[self.fetchedResultsController fetchedObjects]);
NSAssert(!error, @"Error performing fetch request: %@", error);
return _fetchedResultsController;
还有映射:
+(void) prepareMapping
RKObjectManager *manager = [RKObjectManager sharedManager];
NSDictionary *categoryAttributes = @
@"id" : @"remoteId",
@"created_at" : @"updatedAt",
@"created_at" : @"createdAt",
@"name" : @"name",
@"ads_count": @"adsCount",
;
RKEntityMapping *categoryMapping = [RKEntityMapping mappingForEntityForName:@"Category" inManagedObjectStore:manager.managedObjectStore];
[categoryMapping addAttributeMappingsFromDictionary:categoryAttributes];
categoryMapping.identificationAttributes = @[@"remoteId"];
[manager addResponseDescriptorsFromArray:@[
[RKResponseDescriptor responseDescriptorWithMapping:categoryMapping
pathPattern:@"neighborhoods/:neighborhoodId/categories.json"
keyPath:@"index_categories.index_category"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]
]];
【问题讨论】:
【参考方案1】:我不知道NSStringFromClass([Category class])
是否可行。您是否尝试过以下代码?
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Category"];
此外,您可以查看 RestKit v.020-rc1 zip 文件中的示例核心数据项目以了解进展情况。
【讨论】:
【参考方案2】:我想你提到的博客现在已经移到了:http://www.alexedge.co.uk/blog/2013/03/08/introduction-restkit-0-20/
【讨论】:
【参考方案3】:http://nsscreencast.com/episodes/52-restkit-coredata
深入整合
【讨论】:
以上是关于无法让 Restkit 0.20 + CoreData 工作的主要内容,如果未能解决你的问题,请参考以下文章
restkit 0.20:无法将 NSManagedObject 传递给 getObject: 方法