Reputation: 672
Using AFIncrementalStore, I'm trying to fetch an entity's data set when a controller loads. I have something similar working on a previous controller, and attempted to follow that pattern:
- (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];
}
Then later, I try to grab that data to fill a UIPickerView in a table cell:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *rows = [_fetchedResultsController fetchedObjects];
NSLog(@"%@",rows);
...}
But fetchedObjects returns an empty array. The only way I can get data is if I do a refetch just before I call "fetchedObjects" like so:
_fetchedResultsController.fetchRequest.resultType = NSManagedObjectResultType;
[_fetchedResultsController performFetch:nil];
But then I'm hitting the server a second unnecessary time. As far as I can tell, both fetches are executed in the exact same way. So why does it take a second fetch before the data is filled?
My theory is that the second fetch is actually somehow making the first fetch's data available, not actually storing new data, but my understanding of CoreData is a little too shaky for me to determine exactly what is happening. Thanks for helping me sort through this!
edit I meant to mention that I tried the solution to this similar question to no avail: https://stackoverflow.com/a/11334792/380643
Upvotes: 0
Views: 60
Reputation: 672
I discovered I left out the critical delegate method:
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView reloadData];
}
After implementing this, the data came in after the first fetch.
Upvotes: 0