Skip to content

Instantly share code, notes, and snippets.

@bouchtaoui-dev
Created January 12, 2016 13:56
Show Gist options
  • Select an option

  • Save bouchtaoui-dev/cf65974c317d8ee02076 to your computer and use it in GitHub Desktop.

Select an option

Save bouchtaoui-dev/cf65974c317d8ee02076 to your computer and use it in GitHub Desktop.
dispatch_queue_t myQueue = dispatch_queue_create("my queue",NULL);
//...
//...
//...
dispatch_async(imageQueue, ^{
// This will run the method in a separate thread
[self doSomeLongRunningTask];
});
- (void) doSomeLongRunningTask {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"We finished a heavy task!");
});
}
/*
So, we create a dispatch queue handle for our separated thread,
than pass a block ^{} to dispatch_async that will be executed in a separate thread ( NOT in the main thread).
If you want to call a method in the main thread from another thread,
you call: dispatch_async(dispatch_get_main_queue(), ^{ });
As you see, dispatch_get_main_queue() gives you the queue handle of the main thread.
For more information, check:
http://en.wikipedia.org/wiki/Grand_Central_Dispatch
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment