繁体   English   中英

无法使Restkit 0.20 + CoreData工作

[英]Can't make Restkit 0.20 + CoreData work

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

我不知道NSStringFromClass([Category class])是否会工作。 您是否尝试了以下代码?

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Category"];

另外,您可以在RestKit v.020-rc1 zip文件中查看示例核心数据项目,以了解进展情况。

我认为您提到的博客现在已经移至: http : //www.alexedge.co.uk/blog/2013/03/08/introduction-restkit-0-20/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM