使用Firebase`observe`破坏了我在Swift中的dispatchGroup.leave代码,为什么?

时间:2018-10-09 01:39:15

标签: swift firebase firebase-realtime-database

我的enterleave dispatchGroup在我的应用最初运行时没有问题,当{{1}中的user节点发生更改时,它中断了}。

如何有效地处理dispatchGroup?

Firebase

//Gets all Invites received for a user func getInvitesReceived(forUserId forId: String, handler: @escaping ([[String : Any]], Bool) -> ()){ getInvitesReceivedHandle = REF_USERS_INVITES.child(forId).observe(.value) { (snapshot) in var invites = [[String : Any]]()//empty array to hold Invites let dispatchGroup = DispatchGroup()//To track and get notified when the execution has finished before proceeding for item in snapshot.children{ //looping through snapshot made up of sender $uid nodes let itemSnap = item as! DataSnapshot let fromId = itemSnap.key //getting the $uid from the snapshot key let dict = itemSnap.value as! [String : Bool] //getting the child nodes from each sender as dictionary to loop through for (key, status) in dict { //loop throuch each dictionary made up of compliment_id : status switch status {//check status case true: dispatchGroup.enter() //Using helper function to get `User` info by passing `fromId` self.getUserInfo(forUserId: fromId, handler: { (user) in var timeStamp: Int? //Using helper function to get `Compliment` info by passing key = complimentId self.getInviteInfo(forInvitetId: key, handler: { (inviteDict) in timeStamp = inviteDict["timeStamp"] as? Int let invitePack = [ "type": "invite", "complimentId": key, "status": status, "timeStamp": timeStamp!, "fromUser": user ] as [String : Any] invites.append(invitePack) dispatchGroup.leave() })//getComplimentInfo })//end getUserInfo case false: break }//end switch }//end for dict }//end for snapshot.item //get notified of completion then execute handler dispatchGroup.notify(queue: .main) { handler(invites, true) }//end dispatchGroup }//end observe }//end func 个用户节点,当用户节点发生更改时,在上面的observe行崩溃。

dispatchGroup.leave()

编辑:正在读取Firebase结构

enter image description here

0 个答案:

没有答案