如何确定在Swift中运行哪些线程闭包
我正试图准确地确定该线程中的三个闭包是哪个线程 下面的代码在上执行。如何确定在Swift中运行哪些线程闭包,swift,multithreading,closures,Swift,Multithreading,Closures,我正试图准确地确定该线程中的三个闭包是哪个线程 下面的代码在上执行。startLoggingAsync在主线程中被调用,并被传递一个reader参数(闭包1),该参数将在startLoggingAsync完成后的某个时间被调用 闭包2在后台线程上通过GCDDispatchQueue异步调用 闭包3被分配给管道的可读性句柄,只要管道的标准输出上有数据,就会调用该句柄 // // ‘reader’ is CLOSURE 1 passed by caller // func startLoggingA
startLoggingAsync
在主线程中被调用,并被传递一个reader
参数(闭包1),该参数将在startLoggingAsync
完成后的某个时间被调用
闭包2在后台线程上通过GCDDispatchQueue
异步调用
闭包3被分配给管道的可读性句柄
,只要管道的标准输出上有数据,就会调用该句柄
//
// ‘reader’ is CLOSURE 1 passed by caller
//
func startLoggingAsync(withReader reader : @escaping (_ data : String) -> Void) {
. . .
let processQueue = DispatchQueue.global(qos: .background)
processQueue.async { // CLOSURE 2 ran on background thread
self.process = Process()
. . .
let pipe = Pipe()
process.standardOutput = pipe
let outHandle = pipe.fileHandleForReading
outHandle.readabilityHandler = { pipe in // CLOSURE 3
if let string = String(data: pipe.availableData, encoding: .utf8) {
reader(string) // call CLOSURE 1
}
}
. . .
process.waitUntilExit()
outHandle.readabilityHandler = nil
}
}
显然,闭包2在后台线程上运行。闭包1和3在哪些线程上执行 在闭包中添加断点并检查Xcode中的线程?@DominikBucher是的--调试管理器非常清楚--我觉得现在发布这个问题很傻。。。