dispatch_get_main_queue() in main thread
39,009
Solution 1
There isn't a problem with adding an asynchronous block on the main queue from within the main queue, all it does is run the method later on in the run loop.
What you definitely don't want to do is to call dispatch_sync
adding a block to the main queue from within the main queue as you'll end up locking yourself.
Solution 2
Don't worry if you are calling dispatch_async in main thread or not. iOS will put the block in a queue and execute the block in main thread.
Author by
B.S.
Updated on July 09, 2022Comments
-
B.S. almost 2 years
I have method which makes UI changes in some cases.
For example:
-(void) myMethod { if(someExpressionIsTrue) { // make some UI changes // ... // show actionSheet for example } }
Sometimes
myMethod
is called from themainThread
sometimes from someother thread
.Thats is why I want these UI changes to be performed surely in the
mainThread
.I changed needed part of
myMethod
this way:if(someExpressionIsTrue) { dispatch_async(dispatch_get_main_queue(), ^{ // make some UI changes // ... // show actionSheet for example }); }
So the questions:
- Is it safe and good solution to call
dispatch_async(dispatch_get_main_queue()
in main thread? Does it influence on performance? - Can this problem be solved in the other better way? I know that I can check if it is a main thread using
[NSThread isMainThread]
method and calldispatch_async
only in case of other thread, but it will make me create one more method or block with these UI updates.
- Is it safe and good solution to call