Check whether or not the current thread is the main thread

前端 未结 13 1025
北恋
北恋 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:25

    Here is a way to detect what the current queue is
    extension DispatchQueue {
        //Label of the current dispatch queue.
        static var currentQueueLabel: String { String(cString: __dispatch_queue_get_label(nil)) }
    
        /// Whether the current queue is a `NSBackgroundActivityScheduler` task.
        static var isCurrentQueueNSBackgroundActivitySchedulerQueue: Bool { currentQueueLabel.hasPrefix("com.apple.xpc.activity.") }
    
        /// Whether the current queue is a `Main` task.
        static var isCurrentQueueMainQueue: Bool { currentQueueLabel.hasPrefix("com.apple.main-thread") }
    }
    

提交回复
热议问题