Grand Central Dispatch: Multi-Threading With Swift

Difference between dispatch_async and dispatch_sync on serial queue?

Grand Central Dispatch- Swift 3 – Joyce Matos – Medium

GCD (grand central dispatch)

original source : https://blog.bobthedeveloper.io/intro-to-grand-central-dispatch-in-swift-3-with-bob-lee-1d4b56f731b3 

gcd에서는 기본적으로 두가지의 종류의 작업자를 제공한다. (하나는 많은 리소스를 필요로하는 작업에 적합하고(heavy) 하나는 좀더 약한 작업에 적합하다(lightweight). 사용자가 추가로 필요한 경우 lightweigth 작업자를 추가 만들수 있다.)

let main = DispatchQueue.main // heavy 작업자 만들기
let background = DispatchQueue.global() // ios에서 제공하는 lightweight 작업자 만들기
let helper = DispatchQueue(label: "construction_worker_3") // 추가로 lightweight 작업자 만들기
func doSyncWork() { 
background.sync { for _ in 1...3 { print(“Light”) } }
for _ in 1...3 { print(“Heavy”) } } // main
doSyncWork() 
// Light
// Light
// Light
// Heavy
// Heavy
// Heavy

func doAsyncWork() { 
background.async { for _ in 1...3 { print(“Light”) } }
for _ in 1...3 { print(“Heavy”) } }                // main
doAsyncWork() 
// Light
// Heavy
// Heavy
// Light
// Heavy
// Light
let asianWorker = DispatchQueue(label: "construction_worker_1")
let brownWorker = DispatchQueue(label: "construction_worker_2")
func doLightWork() {
asianWorker.async { for _ in 1...10 { print("👷") } }
 brownWorker.async { for _ in 1...10 { print("👷🏽") } } }
doLightWork()
// 👷, 👷🏽, 👷, 👷🏽, 👷, 👷🏽, 👷, 👷🏽, 👷...

함수형태의 gcd 이용 예시

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {  
    // Download file or perform expensive task

    dispatch_async(dispatch_get_main_queue()) {  
        // Update the UI  
    }
}

oop 형태의 gcd 이용 예시

DispatchQueue.global(qos: .userInitiated).async {  
    // Download file or perform expensive task

    DispatchQueue.main.async {  
        // Update the UI  
    }
}

core data 

managed object context

  • performBlock:  Asynchronously performs a given block on the context’s queue.
  • performBlockAndWait:  Synchronously performs a given block on the context’s queue.

Core Data and Swift: Batch Deletes

Core Data and Swift: Batch Updates

Core Data and Swift: Concurrency