Check whether or not the current thread is the main thread

前端 未结 13 1021
北恋
北恋 2020-12-02 11:42

Is there any way to check whether or not the current thread is the main thread in Objective-C?

I want to do something like this.

  - (void)someMethod         


        
13条回答
  •  庸人自扰
    2020-12-02 12:11

    UPDATE: seems that is not correct solution, according to queue.h header as mentioned @demosten

    The first thought was brought to me, when I was needed this functionality was the line:

    dispatch_get_main_queue() == dispatch_get_current_queue();
    

    And had looked to the accepted solution:

    [NSThread isMainThread];
    

    mine solution 2.5 times faster.

    PS And yes, I'd checked, it works for all threads

提交回复
热议问题