2017-12-04 5 views
0

현재 Swift 3.0에서 일하고 있습니다. Dispatch_Queue에 배치 된 함수에서 호출 된 함수가 동일한 Dispatch_Queue에도 배치되는지 또는 Main에 다시 배치되는지 궁금합니다.dispatch_queue의 부모 함수에서 호출 된 함수가 main 또는 dispatch_queue에 배치됩니까?

아래 예제 스 니펫이 포함되어 있습니다.이 경우 parentFunction1()이 MyQueue에 배치되었거나 calledFromParentFunction()이 Main에서 호출 되었기 때문에 calledFromParentFunction()이 MyQueue에서 호출되었는지 궁금합니다. 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() 
    } 
} 
+0

는'Thread' 클래스 var에 isMainThread'로 설명하는 방법이있다. –

+2

달리 수행 할 특정 코드가 없으면 전체 호출 스택이 동일한 큐에 있습니다. – rmaddy

+0

알았어요, 고마워요! – TheMuffinMan834

답변

1

사용자 지정 큐에 있습니다. 이 해킹으로 증명할 수 : Bool` 당신이 특정 코드 경로를 찾아 도움이 될 수 있습니다 :

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

위대한, 나는 많이 생각했다. 그러나 두 번 점검하고 싶었다. 고맙습니다! – TheMuffinMan834

관련 문제