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 Block
、get メソッドで受信したデータを割り当てる方法についてのアイデアがありません。
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;
}