I have several table views that send JSON requests to a server, store the results in core data, and display them using an NSFetchedResultsController
. I was experimenting with GCD as follows:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// Perform JSON requests.
dispatch_async(dispatch_get_main_queue(), ^{
[theTableView reloadData];
});
});
However, this would cause some weird things to happen in the UI. New managed objects would render blank cells, deleted managed objects would cause cells to overlap, etc.
However, I found that if I did this, everything would render correctly.:
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
dispatch_async(dispatch_get_main_queue(), ^{
[theTableView endUpdates];
});
}
What I wanted to know is, why is this necessary? Since it fires as a result of [theTableView reloadData]
, why isn't it automatically included in the main queue? I thought maybe that it was because I didn't call it explicitly. In that case, do I have to wrap all my functions similarly?