RestKit-无法将JSON数据显示到我的tableView上 [英] RestKit - Not been able to display JSON data onto my tableView

查看:136
本文介绍了RestKit-无法将JSON数据显示到我的tableView上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Rest Kit 0.20.3和Xcode 5与核心数据一起使用.我已经从服务器上收到了JSON数据,但是无法在tableView控制器上显示它.

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.

这是AppDelegate.m中的映射和响应描述符代码

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];

和我的tableViewController.m中的GET方法代码

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

以及cellForRowAtIndexPath的代码:

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

我是核心数据的新手,所以我对如何在get方法的Success Block中分配接收到的数据一无所知.

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.

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

推荐答案

使用此行代码

controller.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;

代替

controller.managedObjectContext = self.managedObjectContext;

它将使您的上下文成为主队列上下文.试试吧!

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

这篇关于RestKit-无法将JSON数据显示到我的tableView上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆