ios - DispatchGroup doesn't work as expected -


i have made dispatchgroup , run 2 async tasks. 1 on main , other 1 on global().

as long understand, dispatchgroup.notify's block should invoked once tasks done doesn't work thought.

class que {     let group = dispatchgroup()      init() {         group.notify(queue: .main) {         print("group done")         }     }      func run() {         doc()         dod()     }      fileprivate func doc() {         group.enter()         dispatchqueue.main.async(group: group) {             var rst = 0             idx in 0 ..< 500 {                 rst += idx             }             print("work item c done")             self.group.leave()         }     }      fileprivate func dod() {         group.enter()         dispatchqueue.global().async(group: group) {             var rst = 0             idx in 0 ..< 50 {                 rst += idx             }             print("work item d done")             self.group.leave()         }     } } 

the result is

work item d done group done work item c done 

i want know why not

work item d done work item c done group done 

if made c , d task run on global() queue, worked.

you put call notify in wrong place , called far early.

most want call notify @ end of run method.

init() { }  func run() {     doc()     dod()      group.notify(queue: .main) {         print("group done")     } } 

note says in documentation notify method:

schedules work item submitted queue when group of previously submitted block objects have completed.

the bold mine.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -