简体   繁体   English

主线程中的di​​spatch_get_main_queue()

[英]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: 所以问题:

  • Is it safe and good solution to call dispatch_async(dispatch_get_main_queue() in main thread ? Does it influence on performance? 在主线程中 调用 dispatch_async(dispatch_get_main_queue() 是否安全且良好的解决方案 ?它是否会影响性能?
  • 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 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.

相关问题 dispatch_get_main_queue()的线程ID - thread id of dispatch_get_main_queue() dispatch_get_main_queue的奇怪行为 - Strange behavior of dispatch_get_main_queue 使用dispatch_get_main_queue()是否意味着我的代码将在主线程上? - Does using dispatch_get_main_queue() mean that my code will be on the main thread? “阻止”主线程(dispatch_get_main_queue())和(或不)定期运行 currentRunLoop - 有什么区别? - "Block" main thread (dispatch_get_main_queue()) and (or not) run currentRunLoop periodically - what is the difference? dispatch_async(dispatch_get_main_queue(),^ {}); 从iOS7背景线程慢 - dispatch_async(dispatch_get_main_queue(), ^{}); from Background Thread slow on iOS7 dispatch_sync(dispatch_get_main_queue()UI古怪 - dispatch_sync(dispatch_get_main_queue() UI weirdness 在这种情况下,dispatch_async(dispatch_get_main_queue(),...)是否必要? - Is dispatch_async(dispatch_get_main_queue(), …) necessary in this case? dispatch_get_main_queue块中的代码 - code inside dispatch_get_main_queue block IOS dispatch_get_main_queue()被多次调用 - IOS dispatch_get_main_queue() is called multiple times 包含dispatch_get_main_queue(GCD)的标准库 - Standard library containing dispatch_get_main_queue (GCD)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM