OperationQueue.main与DispatchQueue.main [英] OperationQueue.main vs DispatchQueue.main

查看:396
本文介绍了OperationQueue.main与DispatchQueue.main的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您需要在联网任务或操作的完成块中的主线程上执行某些操作时,哪种方法最合适?为什么?:

When you need to perform something on the main thread in the completion block of a networking task or an operation, which of these ways to get it would be the most appropriate and why?:

  • OperationQueue.main.addOperation
  • DispatchQueue.main.async
  • OperationQueue.main.addOperation
  • DispatchQueue.main.async

推荐答案

有关这两种队列之间的区别的详细信息,请参阅Lion的答案.

For details on the differences between the two types of queue, see Lion's answer.

这两种方法都行得通.但是,当需要更高级的计划(包括依赖项取消等)时,通常需要NSOperation.所以在这种情况下,一个简单的

Both approaches will work. However, NSOperation is mostly needed when more advanced scheduling (including dependencies, canceling, etc.) is required. So in this case, a simple

DispatchQueue.main.async { /* do work */ }

就好了.那等于

dispatch_async(dispatch_get_main_queue(), ^{ /* do work */ });

在Objective-C中,这也是我使用该语言的方式.

in Objective-C, which is also how I would do it in that language.

这篇关于OperationQueue.main与DispatchQueue.main的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆