marcelorocks
marcelorocks

Reputation: 2035

Can't make Restkit 0.20 + CoreData work

I am trying to fetch content from a http json resource (categories list) and connect it with restkit and coredata.

My mapping used to work when I didnt use CoreData. Then I decided to use the following tutorial:

http://www.alexedge.co.uk/portfolio/introduction-restkit-0-20/

However, I am getting a weird error and I just cant find out why:

the entity (null) is not key value coding-compliant for the key "remoteId"

My category model / entity has the remoteId mapped to id on my server, so that is not the issue. However, from the error it seems that Restkit or CoreData cant figure out which entity I'm talking about (they say it is a null entity??)

This is the request code:

- (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;
    }

And the mapping:

    +(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)]
         ]];
    }

Upvotes: 0

Views: 1123

Answers (3)

Live2Enjoy7
Live2Enjoy7

Reputation: 1105

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

Goes in depth of the integration

Upvotes: 0

Trunal Bhanse
Trunal Bhanse

Reputation: 1691

I think the blog you mentioned has now moved to : http://www.alexedge.co.uk/blog/2013/03/08/introduction-restkit-0-20/

Upvotes: 0

flashfabrixx
flashfabrixx

Reputation: 1183

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.

Upvotes: 1

Related Questions