IOS Grand Central Dispatch(GCD)

Grand Central Dispatch(GCD):

Dispatch, also known as Grand Central Dispatch (GCD), contains language features, runtime libraries, and system enhancements that provide systemic, comprehensive improvements to the support for concurrent code execution on multicore hardware in macOS, iOS, watchOS, and tvOS.

DispatchQueue:

An object that manages the execution of tasks serially or concurrently on your app’s main thread or on a background thread.

以下是關於DispatchQueue的種類、sync or async、serial or concurrent、Qos的程式碼

(p.s. 詳細概念說明請參考這個連結)

// Categories: main, global, custom
DispatchQueue.main.async {
    
}
DispatchQueue.global().async {
    
}
DispatchQueue(label: "Custom").async {
    
}

// Sync v.s. async
DispatchQueue.global().sync {
    
}
DispatchQueue.global().async {
    
}

// Serial v.s. Concurrent
DispatchQueue(label: "serial")
DispatchQueue(label: "concurrent", attributes: .concurrent)

//Qos: Quality-of-service classes that specify the priorities for executing tasks.
DispatchQueue(label: "Custom1", qos: .userInteractive)
DispatchQueue(label: "Custom2", qos: .userInitiated)
DispatchQueue(label: "Custom3", qos: .default)
DispatchQueue(label: "Custom4", qos: .utility)
DispatchQueue(label: "Custom5", qos: .background)
DispatchQueue(label: "Custom6", qos: .unspecified)


Ref:

https://franksios.medium.com/ios-gcd%E5%A4%9A%E5%9F%B7%E8%A1%8C%E7%B7%92%E7%9A%84%E8%AA%AA%E6%98%8E%E8%88%87%E6%87%89%E7%94%A8-c69a68d01da1

https://developer.apple.com/documentation/dispatch/dispatchqos/qosclass

https://developer.apple.com/documentation/DISPATCH

探索更多來自 LifeJourney 的內容

立即訂閱即可持續閱讀,還能取得所有封存文章。

Continue reading