且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-06 21:17:34

我解决了这个问题:

    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()
                        }
                    })
                }

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

I would use dispatch group here though so one does not have have to check if its done unnecessarily.