Skip to content

Instantly share code, notes, and snippets.

@landonf
Created January 20, 2011 18:31
Show Gist options
  • Save landonf/788336 to your computer and use it in GitHub Desktop.
Save landonf/788336 to your computer and use it in GitHub Desktop.
// Meanwhile, somewhere in a UI-specific non-thread-safe class
- (void) dealloc {
// glView is not thread-safe and may only be accessed via the main thread.
[_glView release];
[super dealloc];
}
- (void) updateUI {
// Using GCD to execute a heavy operation on a background thread, and then
// report the results on the main thread.
// The dispatch calls copy the provided blocks, which increments self's retain count.
dispatch_async(queue, ^{
NSData *data = [NSData dataWithContentsOfFile: file];
dispatch_async(dispatch_get_main_queue(), ^{
[self displayData: data];
});
});
// Once the blocks have executed, they will be released. If -release/-autorelease is called on a non-main thread and
// the retain count hits 0, our UI classes' non-threadsafe -dealloc will be called on a background thread.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment