Skip to content

Instantly share code, notes, and snippets.

@amleszk
Last active August 29, 2015 14:08
Show Gist options
  • Save amleszk/7f93e55e5cbbf41ec9fb to your computer and use it in GitHub Desktop.
Save amleszk/7f93e55e5cbbf41ec9fb to your computer and use it in GitHub Desktop.
Example of main thread safety for Core data
//RCCoreData helper class
+(BOOL) onMainThread:(void (^)(void))mainThreadOperation
{
BOOL isMainThread = [NSThread isMainThread];
NSAssert(isMainThread, @"Should be on main thread");
if (isMainThread) {
mainThreadOperation();
} else {
dispatch_async(dispatch_get_main_queue(), mainThreadOperation);
}
return isMainThread;
}
// Your tableview NSFetchedResultsController delegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[RCApplication onMainThread:^{
[self.tableView beginUpdates];
}];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[RCApplication onMainThread:^{
[self.tableView endUpdates];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment