【发布时间】:2013-11-10 02:22:29
【问题描述】:
使用 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 的理解有点太不稳定,我无法确定到底发生了什么。感谢您帮我解决这个问题!
edit 我的意思是说我尝试了解决这个类似问题的方法,但无济于事:https://stackoverflow.com/a/11334792/380643
【问题讨论】:
标签: ios objective-c