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