0

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

3 回答 3

1

I don't know if NSStringFromClass([Category class]) will work. Did you try the following code?

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

Additionally you could check out the example core data project inside of the RestKit v.020-rc1 zip file to see how things are going.

于 2013-02-21T21:38:45.337 回答
0

http://nsscreencast.com/episodes/52-restkit-coredata

深入整合

于 2013-06-05T00:48:30.527 回答
0

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

于 2013-06-04T23:52:24.247 回答