At the beginning

  • This is a sub-chapter. For the whole article, see the Learning Diary of Zero-based iOS development

Swift’s version of the GCD

  • Since GCD still changes a lot in Swift, I will rearrange it

Concurrent queue

  • Asynchronous execution
let concurrent = DispatchQueue(label: "concurrent", attributes:.concurrent) for i in 0.. <10 { concurrent.async { print("\(i) ------ \(Thread.current)") } }Copy the code
  • Synchronous execution
let concurrent = DispatchQueue(label: "concurrent") for i in 0.. <10 { concurrent.sync { print("\(i) ------ \(Thread.current)") } }Copy the code
  • Simplified, the second parameter is set to concurrent queue, not serial queue by default
DispatchQueue(label: "concurrent", attributes: .concurrent).async {
    print("\(i) ------ \(Thread.current)")
}
Copy the code

Serial queues

  • Asynchronous execution
let serial = DispatchQueue(label: "serial") for i in 0.. <10 { serial.async { print("\(i) ------ \(Thread.current)") } }Copy the code
  • Synchronous execution
let serial = DispatchQueue(label: "serial") for i in 0.. <10 { serial.sync { print("\(i) ------ \(Thread.current)") } }Copy the code

Primary queue and global queue

  • The home side column
for i in 0.. <10 { DispatchQueue.main.async { print("\(i) ------ \(Thread.current)") } }Copy the code
  • Global queue
let global = DispatchQueue.global() for i in 0.. <10 { global.async { print("\(i) ------ \(Thread.current)") } } let global = DispatchQueue.global() for i in 0.. <10 { global.sync { print("\(i) ------ \(Thread.current)") } }Copy the code

Scheduling group

  • notify
let group = DispatchGroup()
let concurrent = DispatchQueue(label: "concurrent", attributes: .concurrent)
concurrent.async(group: group) {
    Thread.sleep(forTimeInterval: 5)
    print("1-----\(Thread.current)")
}
concurrent.async(group: group) {
        Thread.sleep(forTimeInterval: 5)
        print("2-----\(Thread.current)")
    }
group.notify(queue: DispatchQueue.main) {
    print("finish --- \(Thread.current)")
    print("group end")
}
Copy the code
  • wait
group.wait()
print("group end")
Copy the code
  • The principle of
let group = DispatchGroup()
let concurrent = DispatchQueue(label: "concurrent", attributes: .concurrent)

group.enter()
concurrent.async {
    Thread.sleep(forTimeInterval: 5)
    print("1-----\(Thread.current)")
    group.leave()
}
group.enter()
concurrent.async {
    Thread.sleep(forTimeInterval: 5)
    print("2-----\(Thread.current)")
    group.leave()
}

group.wait()
print("group end")
Copy the code

DispatchWorkItem

  • DispatchWorkItemA piece of pre-executed code
let workItme = DispatchWorkItem {
    Thread.sleep(forTimeInterval: 5)
    print("workItme ----- \(Thread.current)")
    
}
concurrent.async(execute: workItme)
Copy the code

Description of DispatchQueue parameters

DispatchQueue (label: String, / / queue name qos: DispatchQoS, / / service priority attributes: DispatchQueue. Attributes, / / queue type, when you don't set up for serial queue; Concurrent is a concurrent queue; InitiallyInactive need queue activate manual trigger autoreleaseFrequency: DispatchQueue. AutoreleaseFrequency, / / automatic release frequency, inherit the target queue inheritance; WorkItem automatic release pool; Target: DispatchQueue?) // Set the target queue of the queueCopy the code
  • initiallyInactive
let queue = DispatchQueue(label: "concurrent", attributes: [.concurrent, .initiallyInactive])
queue.async {
    print(Thread.current)
}
queue.activate()
Copy the code