ios - dispatch group: for-loop *and* async calls within the for-loop -


i understand how use dispatch group in simple for-loop. i, however, have more complicated for-loop more asynchronous calls within it. want asynchronous calls complete before executing group completion code. have tried apply principles in answer no avail- can use dispatch group not in loop?. here code, based off of techniques observed in link:

let group = dispatchgroup()  ref in self.notifsreflist {     group.enter()     self.db.fetch(withrecordid: ref.recordid) { notifrecord, notiferr in         print("async call")         if notiferr == nil {              // stuff              if let ref = notifrecord?.object(forkey: post) as! ckreference? {                 group.enter()                 self.db.fetch(withrecordid: ref.recordid) { postrecord, err in                     print("async call")                     if err == nil {                         // stuff                         group.leave()                     }                     else {                         print("\(err)")                         group.leave()                     }                 }             }             if let ref = notifrecord?.object(forkey: user_notif) as! ckreference? {                 self.db.fetch(withrecordid: ref.recordid) { userrecord, err2 in                     group.enter()                     print("async call")                     if err2 == nil {                         // stuff                         group.leave()                     }                     else {                         print("\(err2)")                         group.leave()                     }                 }             }             if let ref = notifrecord?.object(forkey: library_item) as! ckreference? {                 self.db.fetch(withrecordid: ref.recordid) { librecord, err3 in                     group.enter()                     print("async call")                     if err3 == nil {                         // stuff                                                 group.leave()                     }                     else {                         print("\(err3)")                         group.leave()                     }                 }             }              group.leave()          }         else {             print("\(notiferr)")             group.leave()         }     }  }   group.notify(queue: .main, execute: { // executed after async calls in loop finish     print("done async calls")     // stuff }) 

from print statements included, know asynchronous calls incorrect: "done async calls" prints before instances of "async call." in how dispatch group working appreciated. thanks!

the problem 2nd , 3rd inner async calls. calling group.enter() inside completion blocks instead of before async calls.

there's no need many calls leave.

you need move 2 follows:

let group = dispatchgroup()  ref in self.notifsreflist {     group.enter()     self.db.fetch(withrecordid: ref.recordid) { notifrecord, notiferr in         print("async call")         if notiferr == nil {              // stuff              if let ref = notifrecord?.object(forkey: post) as! ckreference? {                 group.enter()                 self.db.fetch(withrecordid: ref.recordid) { postrecord, err in                     print("async call")                     if err == nil {                         // stuff                     }                     else {                         print("\(err)")                     }                     group.leave()                 }             }             if let ref = notifrecord?.object(forkey: user_notif) as! ckreference? {                 group.enter()                 self.db.fetch(withrecordid: ref.recordid) { userrecord, err2 in                     print("async call")                     if err2 == nil {                         // stuff                     }                     else {                         print("\(err2)")                     }                     group.leave()                 }             }             if let ref = notifrecord?.object(forkey: library_item) as! ckreference? {                 group.enter()                 self.db.fetch(withrecordid: ref.recordid) { librecord, err3 in                     print("async call")                     if err3 == nil {                         // stuff                                             }                     else {                         print("\(err3)")                     }                     group.leave()                 }             }         }         else {             print("\(notiferr)")         }         group.leave()     } }  group.notify(queue: .main, execute: { // executed after async calls in loop finish     print("done async calls")     // stuff }) 

Comments