1

我正在使用带有核心数据的 Rest Kit 0.20.3 和 Xcode 5。我从我的服务器收到了 JSON 数据,但我无法在 tableView 控制器上显示它。

这是 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 方法代码

-(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 的代码:

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

我是核心数据的新手,所以我不知道如何在Success Blockget 方法中分配接收到的数据。

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

3 回答 3

1

查看此链接https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md

在这里,您将了解如何使用 Rest Kit 处理核心数据。希望它有所帮助,因为它对我有用...

于 2013-11-09T08:49:26.690 回答
0

您不需要将映射结果分配给任何东西。当您使用 Core Data 和实体映射时,RestKit 将在映射过程中为您更新和保存 Core Data 模型。

这一行:

self.fetchedResultsController = [mappingResult firstObject];

没有意义,因为映射结果包含Player类的实例。它也不是直接的数组。

您的获取结果控制器应该是标准配置(就像您在创建新项目时由 Apple 模板创建的那样)。这将自动观察模型并刷新表格视图(由于模型更改时的委托方法回调)。


通过以下方式获取 MOC:

NSManagedObjectContext *moc = [[RKManagedObjectStore defaultStore] mainQueueManagedObjectContext];
于 2013-11-09T08:59:59.897 回答
-1

使用这行代码

controller.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;

代替

controller.managedObjectContext = self.managedObjectContext;

它将使您的上下文成为主队列上下文。试试看!

于 2013-11-09T11:10:08.457 回答