Firebase 查询未按正确顺序执行?
Posted
技术标签:
【中文标题】Firebase 查询未按正确顺序执行?【英文标题】:Firebase query not executing in the correct order? 【发布时间】:2019-07-17 02:25:23 【问题描述】:我获取数据并显示在 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 调用的执行顺序不正确。
【问题讨论】:
【参考方案1】:您应该在设置DispatchGroup
时设置notify
闭包。而且您不需要为您使用完成闭包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
调用是在您设置要通知的任何内容之前发生的。
【讨论】:
我试过这个(作为参数传入),但它不起作用 通知也只运行 1 次。我不知道为什么 通知将仅运行一次。这就是 DispatchGroup 的意义所在——你创建它,使用它,然后处理掉它。如果您需要做其他事情,您可能需要考虑不同的方法。 您可以使用更新后的答案,如果您将使用单独的 DispatchGroups:外层和内层,它可能会起作用。内部的会在创建时调用外部的“进入”,而它们的“通知”将调用“外部的”“离开”。【参考方案2】:我解决了这个问题:
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()
)
我会在这里使用调度组,所以不必检查它是否不必要地完成。
【讨论】:
以上是关于Firebase 查询未按正确顺序执行?的主要内容,如果未能解决你的问题,请参考以下文章