Last active
August 29, 2015 14:08
-
-
Save amleszk/7f93e55e5cbbf41ec9fb to your computer and use it in GitHub Desktop.
Example of main thread safety for Core data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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