从dispatch_queue上的父函数调用的函数是否放在main或dispatch_queue上?

时间:2017-12-04 20:20:58

标签: swift grand-central-dispatch

我目前正在使用Swift 3.0。我想知道放在Dispatch_Queue上的函数调用的函数是否也放在同一个Dispatch_Queue上,或者它们是否放回到Main上?

我在下面包含了一个示例代码段,在这种情况下,我想知道是否从MyQueue调用了calledFromParentFunction(),因为调用它的parentFunction1()放在MyQueue上,或调用了onFromParentFunction(),因为它不是'明确调用MyQueue?

let MyQueue = DispatchQueue(label: "My Queue", attributes: [], target: nil)

parentFunction1() {
    calledFromParentFunction()
}

calledFromParentFunction() {
    print("Is this on Main or MyQueue?")
}

override func viewDidLoad() {
    super.viewDidLoad()
    MyQueue.async { [unowned self] in
        parentFunction1()
    }
}

1 个答案:

答案 0 :(得分:1)

它在自定义队列中。你可以用这个黑客来证明:

func calledFromParentFunction() {
    let name = __dispatch_queue_get_label(nil)
    let queue =  String(cString: name, encoding: .utf8)!
    print("This is on", queue)
}