Junior — Middle
Have you had experience using DispatchGroup in your projects for task synchronization?
sobes.tech AI
Answer from AI
DispatchGroup is used for synchronizing multiple asynchronous tasks in iOS. It allows tracking when a group of tasks has completed and executing code afterward.
Example of usage:
let group = DispatchGroup()
group.enter()
DispatchQueue.global().async {
// Asynchronous task 1
print("Task 1 completed")
group.leave()
}
group.enter()
DispatchQueue.global().async {
// Asynchronous task 2
print("Task 2 completed")
group.leave()
}
group.notify(queue: .main) {
print("All tasks completed")
}
In this example, two asynchronous tasks run in parallel, and after their completion, a notify block is called on the main queue. This is useful for coordinating multiple operations, such as loading data from different sources.