Grand Central Dispatch (GCD) is a framework provided by Apple that was released in 2009 with OS X Snow Leopard & iOS 4. It provides easy to use API for the developers to to run background tasks by creating queue in serial or concurrent style without managing threads by themselves.
GCD abstracts the assignment of threads for computation into dispatch queue. Developers only need to create their own dispatch queue or they can use Apple provided built in global dispatch queue with several built-in Quality of Service (QoS) from user interactive, user initiated, utility, and background. GCD will handle the thread assignment in a thread pool automatically.
There are some instances when we as a developer need to perform multiple batch asynchronous tasks in the background, and then receive the notificationwhen all the job is completed in the future. Apple provides DispatchGroup class that we can use to do this kind of operation. Here are the brief summary of what the DispatchGroup is by Apple.
DispatchGroup allows for aggregate synchronization of work. You can use them to submit multiple different work items and track when they all complete, even though they might run on different queues. This behavior can be helpful when progress can’t be made until all of the specified tasks are complete.
A Scenario when this could be useful is if you have multiple webservice calls that all need to finish before continuing. For example, you need to download multiple sets of data that needs to be processed by some function. You have to wait for all webservices to complete before calling the function to process all the received data
Example: #1//
// ViewController.swift
// DispatchGroup
//
// Created by JOYNAL ABEDIN on 14/8/22.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let dispatchGroup = DispatchGroup()
let queue = DispatchQueue.global(qos: .userInteractive)
queue.async(group: dispatchGroup) {
print(\"✅ Task One Completed..\")
}
queue.async(group: dispatchGroup) {
print(\"✅ Task Two Completed..\")
}
queue.async(group: dispatchGroup) {
print(\"✅ Task Three Completed..\")
}
print(\"🈯️ All Task are finished...\")
}
}
Output:
✅ Task One Completed..
🈯️ All Task are finished...
✅ Task Three Completed..
✅ Task Two Completed..
Above this example we see that, Before completed all task printed \"🈯️ All Task are finished...\" which we didn't want. We want that after finishing all task notify me. Which was the main problem. Solving this problem comes DispatchGroup() notify object which notify when all task completed.
Example: #2
//
// ViewController.swift
// DispatchGroup
//
// Created by JOYNAL ABEDIN on 14/8/22.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let dispatchGroup = DispatchGroup()
let queue = DispatchQueue.global(qos: .userInteractive)
dispatchGroup.enter()
queue.async(group: dispatchGroup) {
sleep(3)
print(\"✅ Task One Completed..\")
dispatchGroup.leave()
}
dispatchGroup.enter()
queue.async(group: dispatchGroup) {
sleep(1)
print(\"✅ Task Two Completed..\")
dispatchGroup.leave()
}
dispatchGroup.enter()
queue.async(group: dispatchGroup) {
sleep(5)
print(\"✅ Task Three Completed..\")
dispatchGroup.leave()
}
dispatchGroup.wait()
dispatchGroup.notify(queue: .main) {
print(\"✳️ Notify All Task Finished ...\")
}
}
}
Output:
✅ Task Two Completed..
✅ Task One Completed..
✅ Task Three Completed..
✳️ Notify All Task Finished ...
Above this example we see that, after completed all task program notify me that \"✳️ Notify All Task Finished ...\" . That's the main reason why we used DispatchGroup()
0 Comments
Leave a Comment