美文网首页fish的iOSIOS理论知识iOS开发实用技术
iOS判断是否在主线程的正确姿势

iOS判断是否在主线程的正确姿势

作者: __LuckyPan__ | 来源:发表于2017-01-11 17:15 被阅读1891次

疑问?


在iOS中,经常需要用到线程的概念,尤其是类似UI绘制等操作,苹果也明确规定必须在主线程进行绘制,那么,我们如何来判断当前所在的线程呢?不同的判断方法又有何区别呢?

1. pthread


pthread是一套通用使用使用C语言编写的多线程的API,可以在Unix / Linux / Windows 等系统跨平台使用。
常用API :

  • pthread_create( )
    /** Explain: 创建一个线程 */
  • pthread_exit ( )
    /** Explain: 退出当前线程 */
  • pthread_main_np ( ) :
    /** Explain: 获取主线程 */

应用 :
if (pthread_main_np()) { // do something in main thread } else { // do something in other thread }
总结:

  1. 能够判断当前是否在主线程,不能判断当前是否在主队列。
  1. 需要引入头文件 #import <pthread.h>

2. NSThread


NSThread是一套苹果公司开发的使用Objective-C编写的多线程API,其功能基本类同于pthread。
 由于GCD本身没有提供判断当前线程是否是主线程的API,因此我们常常使用NSThread中的API代替,这种是我们最常使用的方式。
常用API :

  • @property (readonly) BOOL isMainThread
    /** Explain: 是否是主线程 */

  • - (void)cancel
    /** Explain: 取消线程 */

  • - (void)start
    /** Explain: 开始线程 */

应用:
if ([NSThread isMainThread]) { // do something in main thread } else { // do something in other thread }

这个方法在大部分时间内是有效的,在判断当前是否是主线程的需求下与pthread_main_np()基本类同。
 同样在部分情形中会有问题
总结 :

  1. 该API只会检查当前的线程是否是主线程,不会检查是不是同时在主队列。
  1. 每个app只有一个主线程,但是主线程上运行可能有很多不同的队列在运行。
  2. 如果一个API调用任务在非主队列上的主线程中被执行,会导致某些依赖主队列运行的库出现问题(如VectorKit)。
  3. 不需要引入头文件。

参考一:GCD's Main Queue vs. Main Thread
参考二:[NSThread isMainThread] is probably not what you want
参考三:dispatch_queue_set_specific & dispatch_get_specific

3. dispatch_queue_set_specific() & dispatch_get_specific();


鉴于上面的方法都能够判断当前是否是在主线程,但是都不能判断当前是否在主队列的问题,我们很同意想到,只需要新增对当前队列的判断不是可以呢?
** 常用API :**

  • ~~dispatch_get_current_queue() ~~【iOS6.0之后已被弃用】
    /** Explain: 获得当前队列 */
  • dispatch_queue_set_specific(dispatch_queue_t queue, const void *key,
    void *_Nullable context, dispatch_function_t _Nullable destructor);
    /** Explain: Sets the key/value data for the specified dispatch queue. // 通过设置key/value数据与指定的queue进行关联。 【参数】: queue:需要关联的queue,不允许传入NULL。 key:唯一的关键字。 context:要关联的内容,可以为NULL。 destructor:释放context的函数,当新的context被设置时,destructor会被调用 */
  • dispatch_get_specific(const void *key)
    /** Explain: Returns the value for the key associated with the current dispatch queue. // 根据唯一的key取出当前queue的context,如果当前queue没有key对应的context,则去queue的target queue取,取不着返回NULL,如果对全局队列取,也会返回NULL。 【参数】 key:唯一的关键字。 */

** 应用 :**
static void *mainQueueKey = "mainQueueKey"; dispatch_queue_set_specific(dispatch_get_main_queue(), mainQueueKey, &mainQueueKey, NULL); if (dispatch_get_specific(mainQueueKey)) { // do something in main queue } else { // do something in other queue }
** 总结 :**

  1. 可以实现主队列主线程的判断。
  1. 代码复杂,判断是否是主队列必须提前设定与主队列的关联关系。
  2. 扩展性强,可以用来区分自定义队列。

4. dispatch_queue_get_label()


该方法我是在阅读AFNetworking源码时看到的,第一次见时挺奇怪作者为什么要这么写,看起来好麻烦好复杂,今天,我们就一起瞧瞧这段代码的优点吧。
常用API :

  • dispatch_queue_get_label(dispatch_queue_t queue)
    /** Explain: Returns the label specified for the queue when the queue was created. The label of the queue, or NULL if the queue was not provided a label during initialization. */
  • strcmp(const char *s1, const char *s2)
    /** Explain: strcmp() 用来比较字符串(区分大小写)。 其原型为: int strcmp(const char *s1, const char *s2); 【返回值】: 若str1==str2,则返回零; 若str1<str2,则返回负数; 若str1>str2,则返回正数。 【参数】s1, s2 为需要比较的两个字符串。 【注意】:strcmp() 以二进制的方式进行比较,不会考虑多字节或宽字节字符;如果考虑到本地化的需求,请使用 [strcoll()](http://c.biancheng.net/cpp/html/163.html) 函数。 */
  • DISPATCH_CURRENT_QUEUE_LABEL
    /** Explain: Pass this constant to the [dispatch_queue_get_label] function to retrieve the label of the current queue. */

应用 :
if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) == 0) { // do something in main thread } else { // do something in other thread }
** 总结 :**

  1. 该方法不仅能够判断当前是否是主线程。
  1. 还可用来很好的判断当前是否是在主队列。

参考一:How to get current dispatch queue?

相关文章

网友评论

  • 牧马:判断在主队列有什么意义?
    牧马:@MenThus 主队列必然在主线程执行,主线程执行的任务不一定在主队列,但是实际场景中,都只有要求任务在主线程执行的需求
    MenThus:个人猜测,判断要比异步任务的开销要小,也是处于性能考虑吧。
  • 大亮code:写的不错
    __LuckyPan__:@大亮baby 多谢支持,很久没写了,有空再来更新:smile:
  • yehot:有理有据,赞

本文标题:iOS判断是否在主线程的正确姿势

本文链接:https://www.haomeiwen.com/subject/ggbfuttx.html