0

使用 AFIncrementalStore,我试图在控制器加载时获取实体的数据集。我在以前的控制器上有类似的工作,并试图遵循这种模式:

- (void)viewDidLoad
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ItemCategory"];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedStandardCompare:)]];
fetchRequest.returnsObjectsAsFaults = NO;

_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[appDelegate managedObjectContext] sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
 [_fetchedResultsController performFetch:nil];
}

稍后,我尝试获取该数据以填充表格单元格中的 UIPickerView:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *rows = [_fetchedResultsController fetchedObjects];
NSLog(@"%@",rows);
...}

但是 fetchedObjects 返回一个空数组。我可以获取数据的唯一方法是,如果我在调用“fetchedObjects”之前进行重新获取,如下所示:

_fetchedResultsController.fetchRequest.resultType = NSManagedObjectResultType;
[_fetchedResultsController performFetch:nil];

但是后来我又打了服务器第二次不必要的时间。据我所知,两个提取都以完全相同的方式执行。那么为什么在填充数据之前需要第二次获取呢?

我的理论是第二次获取实际上以某种方式使第一次获取的数据可用,而不是实际存储新数据,但是我对 CoreData 的理解有点太不稳定,我无法确定到底发生了什么。谢谢你帮我解决这个问题!

编辑我的意思是提到我尝试解决这个类似问题无济于事:https ://stackoverflow.com/a/11334792/380643

4

2 回答 2

1

你还记得在你的桌子上做一个 reloadData 吗?

于 2014-01-22T23:48:49.867 回答
0

我发现我遗漏了关键的委托方法:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView reloadData];
}

实现这一点后,数据在第一次获取后进入。

于 2013-12-18T00:42:21.793 回答