Использование Firebase `наблюдаем` нарушает мой код dispatchGroup.leave в Swift, почему? - PullRequest
0 голосов
/ 09 октября 2018

I enter и leave dispatchGroup без проблем, когда мое приложение запускается изначально, оно разрывается при изменении узла user в Firebase.

Как эффективно обработать dispatchGroup?

//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

I observe пользовательский узел и сбой на dispatchGroup.leave() строке выше, когда есть изменение впользовательский узел.

func getUserInfo(forUserId forId: String, handler: @escaping (User) -> ()) {

        REF_USERS.child(forId).observe(.value, with: { (snapshot) in

            //..... handle snapshot

            let user = User(uid: uid, dictionary: dictionary)

            handler(user)

        }) { (error) in
            print(error.localizedDescription)
        }


    }//end func

Редактировать: структура Firebase читается

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...