How to check current thread in Swift 3?

Looks like it's simply Thread.isMainThread in Swift 3.


I've made an extension to print the thread and queue:

extension Thread {
    class func printCurrent() {
        print("\r⚡️: \(Thread.current)\r" + "🏭: \(OperationQueue.current?.underlyingQueue?.label ?? "None")\r")
    }
}

Thread.printCurrent()

The result would be:

⚡️: <NSThread: 0x604000074380>{number = 1, name = main}
🏭: com.apple.main-thread

Also recommend to use:

extension DispatchQueue {
    /// - Parameter closure: Closure to execute.
    func dispatchMainIfNeeded(_ closure: @escaping VoidCompletion) {
        guard self === DispatchQueue.main && Thread.isMainThread else {
            DispatchQueue.main.async(execute: closure)
            return
        }

        closure()
    }
}

DispatchQueue.main.dispatchMainIfNeeded { ... }

Thread.isMainThread will return a boolean indicating if you're currently on the main UI thread. But this will not give you the current thread. It will only tell you if you are on the main or not.

Thread.current will return the current thread you are on.