如何在Swift 3 中查看当前线程?
How to check current thread in Swift 3?
如何查看 Swift 3 中的当前线程是哪一个?
在 Swift 的早期版本中,可以通过这样做来检查当前线程是否是主线程:
NSThread.isMainThread()
看起来只是 Thread.isMainThread
in Swift 3.
Thread.isMainThread
将 return 一个布尔值,指示您当前是否在主 UI 线程上。但这不会给你当前线程。它只会告诉你是否在主线上。
Thread.current
将 return 您所在的当前线程。
我做了一个扩展来打印线程和队列:
extension Thread {
class func printCurrent() {
print("\r⚡️: \(Thread.current)\r" + ": \(OperationQueue.current?.underlyingQueue?.label ?? "None")\r")
}
}
Thread.printCurrent()
结果将是:
⚡️: <NSThread: 0x604000074380>{number = 1, name = main}
: com.apple.main-thread
同时推荐使用:
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 { ... }
在最新的Swift 4.0 ~ 4.2中,我们可以使用Thread.current
见Returns the thread object representing the current thread of execution
Swift 4及以上:
Thread.isMainThread
returns Bool
说明用户是否在主线程上,以防有人想要打印 queue/thread 这个扩展名会有帮助
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
}
}
}
使用方法:
print(Thread.current.threadName)
使用 GCD 时,您可以使用 dispatchPrecondition 来检查进一步执行所需的调度条件。如果你想保证你的代码在正确的线程上执行,这会很有用。例如:
DispatchQueue.main.async {
dispatchPrecondition(condition: .onQueue(DispatchQueue.global())) // will assert because we're executing code on main thread
}
Swift 5+
通常情况下,我们只需要知道代码被调度到哪个队列即可。所以我将 threadName
和 queueName
分成不同的属性以使其更加清晰。
extension Thread {
var threadName: String {
if isMainThread {
return "main"
} else if let threadName = Thread.current.name, !threadName.isEmpty {
return threadName
} else {
return description
}
}
var queueName: String {
if let queueName = String(validatingUTF8: __dispatch_queue_get_label(nil)) {
return queueName
} else if let operationQueueName = OperationQueue.current?.name, !operationQueueName.isEmpty {
return operationQueueName
} else if let dispatchQueueName = OperationQueue.current?.underlyingQueue?.label, !dispatchQueueName.isEmpty {
return dispatchQueueName
} else {
return "n/a"
}
}
}
用例:
DispatchQueue.main.async {
print(Thread.current.threadName)
print(Thread.current.queueName)
}
// main
// com.apple.main-thread
DispatchQueue.global().async {
print(Thread.current.threadName)
print(Thread.current.queueName)
}
// <NSThread: 0x600001cd9d80>{number = 3, name = (null)}
// com.apple.root.default-qos
如何查看 Swift 3 中的当前线程是哪一个?
在 Swift 的早期版本中,可以通过这样做来检查当前线程是否是主线程:
NSThread.isMainThread()
看起来只是 Thread.isMainThread
in Swift 3.
Thread.isMainThread
将 return 一个布尔值,指示您当前是否在主 UI 线程上。但这不会给你当前线程。它只会告诉你是否在主线上。
Thread.current
将 return 您所在的当前线程。
我做了一个扩展来打印线程和队列:
extension Thread {
class func printCurrent() {
print("\r⚡️: \(Thread.current)\r" + ": \(OperationQueue.current?.underlyingQueue?.label ?? "None")\r")
}
}
Thread.printCurrent()
结果将是:
⚡️: <NSThread: 0x604000074380>{number = 1, name = main}
: com.apple.main-thread
同时推荐使用:
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 { ... }
在最新的Swift 4.0 ~ 4.2中,我们可以使用Thread.current
见Returns the thread object representing the current thread of execution
Swift 4及以上:
Thread.isMainThread
returns Bool
说明用户是否在主线程上,以防有人想要打印 queue/thread 这个扩展名会有帮助
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
}
}
}
使用方法:
print(Thread.current.threadName)
使用 GCD 时,您可以使用 dispatchPrecondition 来检查进一步执行所需的调度条件。如果你想保证你的代码在正确的线程上执行,这会很有用。例如:
DispatchQueue.main.async {
dispatchPrecondition(condition: .onQueue(DispatchQueue.global())) // will assert because we're executing code on main thread
}
Swift 5+
通常情况下,我们只需要知道代码被调度到哪个队列即可。所以我将 threadName
和 queueName
分成不同的属性以使其更加清晰。
extension Thread {
var threadName: String {
if isMainThread {
return "main"
} else if let threadName = Thread.current.name, !threadName.isEmpty {
return threadName
} else {
return description
}
}
var queueName: String {
if let queueName = String(validatingUTF8: __dispatch_queue_get_label(nil)) {
return queueName
} else if let operationQueueName = OperationQueue.current?.name, !operationQueueName.isEmpty {
return operationQueueName
} else if let dispatchQueueName = OperationQueue.current?.underlyingQueue?.label, !dispatchQueueName.isEmpty {
return dispatchQueueName
} else {
return "n/a"
}
}
}
用例:
DispatchQueue.main.async {
print(Thread.current.threadName)
print(Thread.current.queueName)
}
// main
// com.apple.main-thread
DispatchQueue.global().async {
print(Thread.current.threadName)
print(Thread.current.queueName)
}
// <NSThread: 0x600001cd9d80>{number = 3, name = (null)}
// com.apple.root.default-qos