Skip to content

Instantly share code, notes, and snippets.

@LeoDeMatos
Created July 6, 2019 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeoDeMatos/c237512f797d2a858e8951d8f2d85eb6 to your computer and use it in GitHub Desktop.
Save LeoDeMatos/c237512f797d2a858e8951d8f2d85eb6 to your computer and use it in GitHub Desktop.
DispatchGroup Example, you can copy and paste this code to your swift playground to see it working.
import Foundation
func doMultipleWork() {
var items: [Int] = []
let group = DispatchGroup()
group.enter()
doSomeWork(position: 1) {
items.append(1)
group.leave()
}
group.enter()
doSomeWork(position: 2) {
items.append(2)
group.leave()
}
// When all work is done this gest called
group.notify(queue: .main) {
print(String(describing: items))
}
}
func doSomeWork(position: Int, completion: () -> Void) {
completion()
}
doMultipleWork()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment