简体   繁体   English

使用dispatch_async + dispatch_get_main_queue进行UI更新有什么意义?

[英]What's the point in using dispatch_async + dispatch_get_main_queue for UI updates?

I'm seeing quite a lot of dispatch blocks after successful network requests using AFNetworking (with blocks) in the codebase I need to work with: 在我需要使用的代码库中使用AFNetworking(带有块)成功的网络请求后,我看到了很多调度块:

-(void)someMethodThatPerformsNetworkRequest{

  networkManager.newsList getNewsCallBack:^{

    // Store received and parsed data...

    // Do UI updates. IMHO it's not necessary to warp it inside dispatch_async
    dispatch_async(dispatch_get_main_queue(), ^(){

      // Some UI updates
      // ... table reloads, label changes and etc.

    });
  }
}

From what I know, we don't need explicitly to set callback code to be executed on main thread because AFNetworking already performs callbacks on main thread. 据我所知,我们不需要显式设置要在主线程上执行的回调代码,因为AFNetworking已经在主线程上执行回调。 The only reason I see here is to allow parent method to finish its work and "a little bit" to delay these UI updates (putting them into the end of main thread queue). 我在这里看到的唯一原因是允许父方法完成其工作并“一点点”延迟这些UI更新(将它们放入主线程队列的末尾)。 But we probably don't need do that, so these dispatch_async should be removed IMHO. 但我们可能不需要那样做,所以这些dispatch_async应该被删除恕我直言。 Any ideas on that? 有什么想法吗?

Generally for asynchronous network requests, this is necessary, because the callbacks happen on a background thread. 通常,对于异步网络请求,这是必要的,因为回调发生在后台线程上。 However, AFNetworking should always be calling the response blocks on the main thread, so it should be completely unnecessary to do this in your own code when using AFNetworking. 但是,AFNetworking应始终在主线程上调用响应块,因此在使用AFNetworking时,在您自己的代码中完成此操作应该完全没必要。 It may have been that the code you're using previously used a different library where the callbacks didn't come on the main thread, or used custom written code even, so it's probably just leftover. 可能是您以前使用的代码使用了不同的库,其中回调没有出现在主线程上,或者甚至使用自定义编写的代码,所以它可能只是剩下的。 Either that or the previous developer didn't know that the AFNetworking callbacks came on the main thread. 无论是那个还是以前的开发人员都不知道AFNetworking回调出现在主线程上。 So it should be safe to remove it. 所以删除它应该是安全的。

Any framework or library that is across-the-board calling blocks on specific threads, without being incredibly obvious about it, is doing something very wrong, IMHO. 恕我直言,任何框架或库都是特定线程上的全面调用块,而不是非常明显,它正在做一些非常错误的事情。

However, in AFNetworking's specific case it looks like they took the 80/20 rule -- the default callback queue is on the main thread (it's using NSOperationQueue behind the scenes). 但是,在AFNetworking的特定情况下,看起来它们采用了80/20规则 - 默认回调队列在主线程上(它在后台使用NSOperationQueue)。 In other words, AFNetworking does not call blocks on the main queue -- it calls them on the configured queue. 换句话说, AFNetworking不会调用主队列上的块 - 它会在配置的队列上调用它们。 If the default configured queue is not modified or set by the end developer, then the operation runs on the main thread. 如果未由最终开发人员修改或设置默认配置队列,则操作将在主线程上运行。

To change this, simply set the completionQueue on the AFHTTPRequestOperation instance. 要更改此设置,只需在AFHTTPRequestOperation实例上设置completionQueue即可。

If you have a very heavily threaded app, you may likely be managing your own work queue, which you pass to AFNetworking. 如果你有一个非常强大的线程应用程序,你可能会管理自己的工作队列,并将其传递给AFNetworking。 Especially in these cases, dispatching the UI updates on the main thread is vital (as it always is). 特别是在这些情况下,在主线程上调度UI更新至关重要(因为它始终如此)。

Yes AFNetworking blocks are executed on main thread until you set completionQueue to AFHTTPRequestOperation. 是AFNetworking块在主线程上执行,直到您将completionQueue设置为AFHTTPRequestOperation。 If you are not doing anything fancy (like full background network code that should not lock the main thread) you don't have to use that block in your logic. 如果您没有做任何花哨的事情(例如不应该锁定主线程的完整后台网络代码),则不必在逻辑中使用该块。

Network operations of course are done in separate thread not locking until response comes, but completion and failure blocks are executed always on main thread by default. 网络操作当然是在单独的线程中完成,直到响应到来才锁定,但默认情况下总是在主线程上执行完成和失败块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用“ dispatch_async(dispatch_get_main_queue(),^ {block})更新UI - Updating the UI using "dispatch_async(dispatch_get_main_queue(),^{block}) 在UI线程上运行dispatch_async(dispatch_get_main_queue()的目的 - Purpose of running dispatch_async(dispatch_get_main_queue() on UI Thread 在什么情况下dispatch_async(dispatch_get_main_queue(),^ ...什么都不做 - Under what circumstances does dispatch_async(dispatch_get_main_queue(), ^ … do nothing 嵌套dispatch_async(dispatch_get_main_queue()^ {})的目的是什么? - What is the purpose of nesting dispatch_async(dispatch_get_main_queue()^{}) ? 直接执行方法与在dispatch_async(dispatch_get_main_queue(),^ {})块中有什么区别 - What is the difference between perform a method directly and in the block of dispatch_async(dispatch_get_main_queue(), ^{}) 调用dispatch_async(dispatch_get_main_queue())时UIView屏幕冻结 - UIView screen freeze when calling dispatch_async(dispatch_get_main_queue()) 从xctest执行dispatch_async(dispatch_get_main_queue() - Execute dispatch_async(dispatch_get_main_queue() from xctest NSMutableURLRequest超时后未触发dispatch_async(dispatch_get_main_queue() - dispatch_async(dispatch_get_main_queue() not firing after NSMutableURLRequest with timeoutInterval NSManagedObjectContext中的dispatch_async(dispatch_get_main_queue()在performBlock中 - dispatch_async(dispatch_get_main_queue() inside NSManagedObjectContext performBlock dispatch_async(dispatch_get_main_queue(),^ {...});等到完成? - Does dispatch_async(dispatch_get_main_queue(), ^{…}); wait until done?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM