我正在尝试从 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)]
]];
}