2016-06-29 28 views
4

這是一個直接的問題。默認情況下,是DispatchQueue .serial還是.concurrent?

let serial = DispatchQueue(label: "serial", attributes: .serial) 
let concurrent = DispatchQueue(label: "concurrent", attributes: .concurrent) 
let q = DispatchQueue(label: "q") 

我看不到任何財產,我可以檢查q,這將告訴我。

PlaygroundPage.current.needsIndefiniteExecution = true一起在運動場上跑步顯示連續行爲,但我不想依賴操場(與異步的東西類似),或無證行爲。

任何人都可以提供一個硬鏈接到文檔的答案嗎?

回答

5

在Swift 3之前,默認的調度隊列類型是串行的 - passing nil into the attributes parameterdispatch_queue_create會產生一個串行隊列,而且我看不到默認隊列類型要更改的原因。雖然不幸的是我找不到有關DispatchQueue的任何文件,可以證實這一點。

然而,looking at the source code表明,這確實仍是如此:

public convenience init(
    label: String, 
    attributes: DispatchQueueAttributes = .serial, 
    target: DispatchQueue? = nil) 
{ 
    ... 
} 

雖然我總是喜歡顯式地指定屬性,使我的代碼更清晰,防止這樣的困惑。

+0

源代碼鏈接很棒,謝謝。我在cmd +單擊xcode中的方法名稱時看不到默認參數實現。 – SimplGy

+0

@SimplGy樂於幫助:) – Hamish