GCD

  • Task + Queue
  • Ease of use, efficiency, performance
  • Source: opensource.apple.com/tarballs/li…

features

  • Creating a management Queue
  • Submit the Job
  • Dispatch Group
  • Management Dispatch Object
  • The signal Semaphore
  • Queue Barrier
  • Dispatch Source
  • The Queue Context data
  • Dispatch IO Channel
  • Dispatch Data object

The GCD queues

  • Main queue: Tasks are executed on the main thread

  • Parallel queue columns: Tasks are placed in and out of the queue in a first-in, first-out order, but because multiple tasks can be executed in parallel, the order in which they are completed varies

  • Serial queue: Tasks are out and in with a fifO property, but only one task is executed at a time

  • API

    • Dispatch.main
    • Dispatch.global
    • DispatchQueue(label:, qos:, attributes:, autorealeaseFrequency:, target:, )
    • queue.label;
    • setTarget(queue: DispatchQueue?)

  • The final destination queue is the main queue and the global queue
  • If the target queue of a parallel queue is set to a serial queue, the parallel queue will no longer be parallel
  • If the target queues of the song queue are all set to the same serial queue, then the multiple queues, together with the tasks in the target queue, will be executed sequentially
  • If the set target queue forms a ring, the result is unpredictable
  • If the target queue is changed while a task is being performed in one queue, the result is also unpredictable

GCD Basic operation

  • Synchronous and Asynchronous
  • sync
    • Submit the task to the current queue, and the current queue will not return until the task is completed
  • async
  • asyncAfter
    • How long to schedule a task to execute, but not until the task has finished executing before the current queue returns
let queue = DispatchQueue(label: "myqyeue", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)

queue.sync {
    print("in queue sync")
}
queue.async {
    sleep(1)
    print("in queue async")
}

queue.asyncAfter(deadline: .now() + 1) {
    print("in asyncAfter")}print("after invoke queue")
Copy the code

GCD Advanced features

  • DispatchGroup

let workingGroup = DispatchGroup(a)let workingQueue = DispatchQueue(label: "request_queue")

workingGroup.enter()
workingQueue.async {
    Thread.sleep(forTimeInterval: 1)
    print("Data request completed for interface A")
    workingGroup.leave()
}

workingGroup.enter()
workingQueue.async {
    Thread.sleep(forTimeInterval: 1)
    print("Interface B data request completed")
    workingGroup.leave()
}

print("I'm the first to execute, the print after execution in the asynchronous operation.")
workingGroup.wait()
print("Data requests for interfaces A and B complete, start merging data for both interfaces")
Copy the code
  • Dispatch_notify
let workingGroup = DispatchGroup(a)let workingQueue = DispatchQueue(label: "request_queue")

workingGroup.enter()
workingQueue.async {
    Thread.sleep(forTimeInterval: 1)
    print("Data request completed for interface A")
    workingGroup.leave()
}

workingGroup.enter()
workingQueue.async {
    Thread.sleep(forTimeInterval: 1)
    print("Interface B data request completed")
    workingGroup.leave()
}

print("I'm the first to execute, the print after execution in the asynchronous operation.")
workingGroup.notify(queue: workingQueue) {
    print("Data requests for interfaces A and B complete, start merging data for both interfaces")}print("Verify not blocking")
Copy the code
  • DispatchSource
    • A Dispatch Source is an object that listens for certain types of events, and when those events occur, it automatically puts a task into the execution routine of a Dispatch Queue
    • There are many events
let queue = DispatchQueue(label: "request_queue")
var seconds = 10
let timer = DispatchSource.makeTimerSource(flags: [], queue: queue)
timer.schedule(deadline: .now(), repeating: 1.0)
timer.setEventHandler {
    seconds - = 1
    if seconds < 0 {
        timer.cancel()
    }else{
        print(seconds)
    }
}
timer.resume()
Copy the code