DispatchGroup Enter, Leave and Notify in Simple Example
let dispatch = DispatchGroup()
dispatch.enter()
f1(){
// do sth
dispatch.leave()
}
dispatch.enter()
f2(){
// do sth
dispatch.leave()
}
dispatch.notify(queue:queue){
// f1() and f2() complete
// it is time to do sth awesome!
}
DispatchGroup Enter, Leave and Wait Example
// Apple Grand Center Dispatch(GCD) in Swift 4
// You can use them to submit multiple work item and track when they all complete.
// if we want to run the nodes in following order: node1.run(..) => node2.run(..)
// then we need to use Apple GCD dispatchGroup, enter, leave and wait
let dispatch = DispatchGroup()
// dispatch.enter()
//
// dispatch.leave()
//
// dispatch.wait()
// We can do sth useful now.
let act1 = SKAction.move(..)
let sequence = SKAction.sequence([act1], ...)
for node in [node1, node2]{
dispatch.enter()
node.run(sequence, completion:{
dispatch.leave()
})
dispatch.wait()
}
// running order: node1.run(..) done => node2.run(..) done