Firebase查询未按正确顺序执行?

时间:2019-07-17 02:25:23

标签: ios swift firebase firebase-realtime-database dispatch-queue

我获取数据并显示在tableView中,问题是数据没有以正确的顺序执行。

我尝试过:

            for case let child as DataSnapshot in data!.children.reversed() {
                let newDispatchGroup = DispatchGroup()
                let commentID = child.key
                let uid = child.childSnapshot(forPath: "UID").value as! String
                let commentText = child.childSnapshot(forPath: "Comment").value!
                let timeStamp = child.childSnapshot(forPath: "timeStamp").value!
                let date = ConvertDate(mediaTimestamp: timeStamp as! Double).getDate!
                //print(date, "dsfsdafdasfdsafdsahjkfhfdsafsajkadhffdsfsafsasjkfhsdajkhfdsajkhfjklads")

                newDispatchGroup.enter()

                ref.child("users2").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
                    print(snapshot, "dshjkfhjkadhfsjkfhsdajkhfdsajkhfjklads")
                    print(date, "dsfsdafdasfdsafdsahjkfhfdsafsajkadhffdsfsafsasjkfhsdajkhfdsajkhfjklads")

                    let username = snapshot.childSnapshot(forPath: "username").value
                    let profileImage = snapshot.childSnapshot(forPath: "profileImage").value

                    let newUser = User(theuserID: uid, theUsername: username as! String, theprofImage: profileImage as! String)

                    let newComment = Comment(newUser: newUser, text: commentText as! String, timeStamp: date, NcommentID: commentID)
                    self.commentsVC1.arrayOfComments.append(newComment)
                    newDispatchGroup.leave()
                    //completion()
                })
                newDispatchGroup.notify(queue: .main, execute: {
                    print(self.totalComments, "COgfdsdfgfdsgdsfgdfsgfdsgdfsgdskj", self.commentsVC1.arrayOfComments.count)
                    if self.totalComments == self.commentsVC1.arrayOfComments.count {
                        print("COmejkfbdshkafdsagfhksdagfdsakj")
                        self.commentsVC1.tableView.reloadData()
                    }
                })
            }
        })
    }

但是它也不起作用,第二个Firebase调用的执行顺序不正确。

2 个答案:

答案 0 :(得分:0)

我解决了这个问题:

    for case let child as DataSnapshot in snap.children.reversed() {
                    let commentID = child.key
                    let uid = child.childSnapshot(forPath: "UID").value as! String
                    let commentText = child.childSnapshot(forPath: "Comment").value!
                    let timeStamp = child.childSnapshot(forPath: "timeStamp").value!
                    let date = ConvertDate(mediaTimestamp: timeStamp as! Double).getDate!
                    let newUser = User(theuserID: uid)
                    let newComment = Comment(newUser: newUser, text: commentText as! String, timeStamp: date, NcommentID: commentID)
                    self.commentsVC1.arrayOfComments.append(newComment)
                    ref.child("users2").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in

                        let username = snapshot.childSnapshot(forPath: "username").value
                        let profileImage = snapshot.childSnapshot(forPath: "profileImage").value

                        let newUserIner = User(theuserID: uid, theUsername: username as! String, theprofImage: profileImage as! String)
                        newComment.user = newUserIner
                        if self.totalComments == self.commentsVC1.arrayOfComments.count {
                            self.commentsVC1.tableView.reloadData()
                        }
                    })
                }

尽管我会在这里使用调度组,所以不必检查它是否不必要地完成了。

答案 1 :(得分:-1)

设置notify时,应设置DispatchGroup闭包。并且您无需为loadComments函数使用完成闭包。

let dispatchGroup = DispatchGroup()
dispatchGroup.notify(queue: .main, execute: {
    if self.totalComments == self.commentsVC1.arrayOfComments.count {
        print("COmejkfbdshkafdsagfhksdagfdsakj")
        self.commentsVC1.tableView.reloadData()
    }
})

loadComments()
notify被调用与leave相同的次数时,将调用

enter。在您的代码中,最后一个leave调用发生在您设置要通知的内容之前。