I have been using with success, grand central dispatch in my apps, but I was wondering what is the real advantage of using something like this:
dispatch_async(dispatch_get_main_queue(), ^{ ... do stuff
or even
dispatch_sync(dispatch_get_main_queue(), ^{ ... do stuff
I mean, in both cases you are firing a block to be executed on the main thread, exactly where the app runs and this will not help to reduce the load. In the first case you don't have any control when the block will run. I have seen cases of blocks being executed half a second after you fire them. The second case, it is similar to
[self doStuff];
right?
I wonder what do you guys think.
This question is related to
ios
concurrency
grand-central-dispatch
dispatch-async
Async means asynchronous and you should use that most of the time. You should never call sync on main thread cause it will lock up your UI until the task is completed. You Here is a better way to do this in Swift:
runThisInMainThread { () -> Void in
// Run your code like this:
self.doStuff()
}
func runThisInMainThread(block: dispatch_block_t) {
dispatch_async(dispatch_get_main_queue(), block)
}
Its included as a standard function in my repo, check it out: https://github.com/goktugyil/EZSwiftExtensions
One place where it's useful is for UI activities, like setting a spinner before a lengthy operation:
- (void) handleDoSomethingButton{
[mySpinner startAnimating];
(do something lengthy)
[mySpinner stopAnimating];
}
will not work, because you are blocking the main thread during your lengthy thing and not letting UIKit actually start the spinner.
- (void) handleDoSomethingButton{
[mySpinner startAnimating];
dispatch_async (dispatch_get_main_queue(), ^{
(do something lengthy)
[mySpinner stopAnimating];
});
}
will return control to the run loop, which will schedule UI updating, starting the spinner, then will get the next thing off the dispatch queue, which is your actual processing. When your processing is done, the animation stop is called, and you return to the run loop, where the UI then gets updated with the stop.
Hopefully I'm understanding your question correctly in that you are wondering about the differences between dispatch_async and dispatch_sync?
dispatch_async
will dispatch the block to a queue asynchronously. Meaning it will send the block to the queue and not wait for it to return before continuing on the execution of the remaining code in your method.
dispatch_sync
will dispatch the block to a queue synchronously. This will prevent any more execution of remaining code in the method until the block has finished executing.
I've mostly used a dispatch_async
to a background queue to get work off the main queue and take advantage of any extra cores that the device may have. Then dispatch_async
to the main thread if I need to update the UI.
Good luck
Dispatching blocks to the main queue from the main thread can be useful. It gives the main queue a chance to handle other blocks that have been queued so that you're not simply blocking everything else from executing.
For example you could write an essentially single threaded server that nonetheless handles many concurrent connections. As long as no individual block in the queue takes too long the server stays responsive to new requests.
If your program does nothing but spend its whole life responding to events then this can be quite natural. You just set up your event handlers to run on the main queue and then call dispatch_main(), and you may not need to worry about thread safety at all.
Swift 3, 4 & 5
Running code on the main thread
DispatchQueue.main.async {
// Your code here
}
Source: Stackoverflow.com