ios 如何在 Swift 3 中检查当前线程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39307800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:02:39  来源:igfitidea点击:

How to check current thread in Swift 3?

iosmultithreadingswift3nsthread

提问by BalestraPatrick

How do I check which one is the current thread in Swift 3?

如何检查 Swift 3 中哪个是当前线程?

In previous versions of Swift it was possible to check if the current thread was the main one by doing this:

在以前的 Swift 版本中,可以通过执行以下操作来检查当前线程是否是主线程:

NSThread.isMainThread()

回答by BalestraPatrick

Looks like it's simply Thread.isMainThreadin Swift 3.

看起来它只是Thread.isMainThread在 Swift 3 中。

回答by Brandon A

Thread.isMainThreadwill 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.isMainThread将返回一个布尔值,指示您当前是否在主 UI 线程上。但这不会给你当前的线程。它只会告诉你你是否在主要或不。

Thread.currentwill return the current thread you are on.

Thread.current将返回您所在的当前线程。

回答by Nik Kov

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()

Thread.printCurrent()

The result would be:

结果将是:

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

回答by Suhit Patil

Swift 4 and above:

Swift 4 及以上:

Thread.isMainThreadreturns Boolstating that if the user is on Main Thread or Not, in case if someone wants to print the name of the queue/thread this extension will be helpful

Thread.isMainThread返回Bool说明用户是否在主线程上,如果有人想打印队列/线程的名称,此扩展将很有帮助

extension Thread {

    var threadName: String {
        if let currentOperationQueue = OperationQueue.current?.name {
            return "OperationQueue: \(currentOperationQueue)"
        } else if let underlyingDispatchQueue = OperationQueue.current?.underlyingQueue?.label {
            return "DispatchQueue: \(underlyingDispatchQueue)"
        } else {
            let name = __dispatch_queue_get_label(nil)
            return String(cString: name, encoding: .utf8) ?? Thread.current.description
        }
    }
}

How to use:

如何使用:

print(Thread.current.threadName)

回答by Maxim Makhun

When using GCD you can use dispatchPreconditionto check a dispatch condition necessary for further execution. This can be useful if you want to guarantee your code execution on correct thread. For example:

使用 GCD 时,您可以使用dispatchPrecondition检查进一步执行所需的调度条件。如果您想保证在正确的线程上执行代码,这会很有用。例如:

DispatchQueue.main.async {
    dispatchPrecondition(condition: .onQueue(DispatchQueue.global())) // will assert because we're executing code on main thread
}

回答by code4latte

In latest Swift 4.0 ~ 4.2, we can use Thread.current

在最新的 Swift 4.0 ~ 4.2 中,我们可以使用 Thread.current

See Returns the thread object representing the current thread of execution

请参见返回表示当前执行线程的线程对象