Skip to content

Instantly share code, notes, and snippets.

@Galarius
Last active August 29, 2015 14:09
Show Gist options
  • Save Galarius/5d63c29c06ffe264a68b to your computer and use it in GitHub Desktop.
Save Galarius/5d63c29c06ffe264a68b to your computer and use it in GitHub Desktop.
multithreading in iOS
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
// Delay execution of block for 1 second.
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Execution of block in the background thread.
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Execution of block in the background thread.
dispatch_async(dispatch_get_main_queue(), ^{
// Execution of block in the main thread.
});
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Execution of block in the background thread.
dispatch_sync(dispatch_get_main_queue(), ^{
// Execution of block in the main thread.
// Wait until the end.
});
// Continue execution in the background thread.
});
static dispatch_once_t task;
dispatch_once(&task, ^{
// Single execution of block.
});
int times = 7;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_apply(times, queue, ^(size_t index) {
// Execution of block for 'times' times.
// Parallelization with 'index' parameter.
// Wait until all done.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment