Created
July 6, 2019 20:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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