[英]dispatch_get_main_queue() in main thread
I have method which makes UI changes in some cases. 我有一些方法可以在某些情况下改变UI。
For example: 例如:
-(void) myMethod {
if(someExpressionIsTrue) {
// make some UI changes
// ...
// show actionSheet for example
}
}
Sometimes myMethod
is called from the mainThread
sometimes from some other thread
. 有时
myMethod
有时是从mainThread
调用来自other thread
一些other thread
。
Thats is why I want these UI changes to be performed surely in the mainThread
. 这就是为什么我希望在
mainThread
确实执行这些UI更改。
I changed needed part of myMethod
this way: 我用这种方式改变了
myMethod
所需的部分:
if(someExpressionIsTrue) {
dispatch_async(dispatch_get_main_queue(), ^{
// make some UI changes
// ...
// show actionSheet for example
});
}
So the questions: 所以问题:
dispatch_async(dispatch_get_main_queue()
in main thread ? Does it influence on performance? dispatch_async(dispatch_get_main_queue()
是否安全且良好的解决方案 ?它是否会影响性能? [NSThread isMainThread]
method and call dispatch_async
only in case of other thread, but it will make me create one more method or block with these UI updates. [NSThread isMainThread]
方法检查它是否是主线程,并且仅在其他线程的情况下调用dispatch_async
,但它将使我创建一个具有这些UI更新的方法或块。 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. 你绝对不想做的是调用
dispatch_sync
从主队列中向主队列添加一个块,因为你最终会锁定自己。
Don't worry if you are calling dispatch_async in main thread or not. 如果您在主线程中调用dispatch_async,请不要担心。 iOS will put the block in a queue and execute the block in main thread.
iOS会将块放入队列并在主线程中执行块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.