RestKit - Not been able to display JSON data onto

2019-05-31 07:23发布

问题:

I'm using Rest Kit 0.20.3 and Xcode 5 with core Data. I've recieved the JSON data from my server but I'm not able to display it on the tableView controller.

Here is mapping and response descriptor code in AppDelegate.m

RKEntityMapping *playerMapping = [RKEntityMapping mappingForEntityForName:@"Player" inManagedObjectStore:managedObjectStore];

    [playerMapping addAttributeMappingsFromDictionary:@{@"id": @"playerID",
                                                        @"name": @"playerName",
                                                        @"age": @"playerAge",
                                                        @"created_at": @"createdAt",
                                                        @"updated_at": @"updatedAt"}];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:playerMapping method:RKRequestMethodGET pathPattern:@"/players.json" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    [objectManager addResponseDescriptor:responseDescriptor];

and my code for GET method in my tableViewController.m

-(void)loadPlayers{
    [[RKObjectManager sharedManager] getObjectsAtPath:@"/players.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
        [self.refreshControl endRefreshing];
        NSLog(@"IT WORKED!");
        self.fetchedResultsController = [mappingResult firstObject];
    }failure:^(RKObjectRequestOperation *operation, NSError *error){
        NSLog(@"FAILED TO PERFORM GET");
    }];
}

and code for cellForRowAtIndexPath :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    Player *player = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = player.playerName;
    return cell;
}

I'm new with core data so I don't have an Idea on how to assign the received data in the Success Block of the get method.

code for FRC:

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"playerName" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}

回答1:

Use This line of code

controller.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;

Instead of

controller.managedObjectContext = self.managedObjectContext;

It will make your context the main queue context. Try it!



回答2:

look into this link https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md

And here you will get how to work with Core data with Rest Kit.. Hope it Helps, as it worked for me...



回答3:

You don't need to assign the mapping result to anything. When you use Core Data and entity mapping the Core Data model will be updated and saved by RestKit for you during the mapping process.

This line:

self.fetchedResultsController = [mappingResult firstObject];

Doesn't make sense because the mapping result contains instances of Player class. It also isn't directly an array.

Your fetched results controller should be a standard configuration (just like the one created by the Apple template when you create a new project). This will automatically observe the model and refresh the table view (due to the delegate method callbacks when the model changes).


Get the MOC with:

NSManagedObjectContext *moc = [[RKManagedObjectStore defaultStore] mainQueueManagedObjectContext];