在所有Firebase调用完成后,如何重新加载数据? [英] How to reload data after all Firebase calls finished?
问题描述
我正在使用Firebase(Swift)读取用户所属的组ID列表,然后遍历ID以获取有关组的更多信息.与此类似的东西(伪代码):
I'm using Firebase (Swift) to read a list of group ids that the user belongs to then looping over the ids to get more info about the groups. Something similar to this (pseudo code):
// List the names of all Mary's groups
var ref = new Firebase("https://docs-examples.firebaseio.com/web/org");
// fetch a list of Mary's groups
ref.child("users/mchen/groups").on('child_added', function(snapshot) {
// for each group, fetch the name and print it
String groupKey = snapshot.key();
ref.child("groups/" + groupKey + "/name").once('value', function(snapshot) {
System.out.println("Mary is a member of this group: " + snapshot.val());
});
});
我怎么知道所有Firebase observeSingleEvent
已完成执行,所以我可以在集合视图中重新加载数据.
How do I know that all Firebase observeSingleEvent
has done executing so I could reload the data in my collection view.
经过更多研究后,这看起来与问题非常相似.我可以使用dispatch_group
或螺栓框架
After doing more research, this looks very similar to this question. I could use dispatch_group
or Bolts framework
感谢@appzYourLife的回答.我也能够使用RxSwift
解决它.我只是将Firebase调用与观察者包装在一起,并将它们保存在一个名为
Thanks to @appzYourLife for his answer. I also was able to solve it using RxSwift
. I simply wrapped the Firebase calls with observers and saved them in an array then called
Observable.zip(observables, { _ in }).subscribe(onCompleted: {
self.contentView.collection.reloadData() // do something here
})
推荐答案
如果您希望在完成所有Firebase调用时收到通知,则可以使用此代码
If you want to be notified when all the firebase calls have been completed you can use this code
let ref = FIRDatabase.database().reference()
ref.child("users/mchen/groups").observeSingleEvent(of: .value, with: { snapshot in
let groupKeys = snapshot.children.flatMap { $0 as? FIRDataSnapshot }.map { $0.key }
// This group will keep track of the number of blocks still pending
let group = DispatchGroup()
for groupKey in groupKeys {
group.enter()
ref.child("groups").child(groupKey).child("name").observeSingleEvent(of: .value, with: { snapshot in
print("Mary is a member of this group: \(snapshot.value)")
group.leave()
})
}
// We ask to be notified when every block left the group
group.notify(queue: .main) {
print("All callbacks are completed")
}
})
它如何工作?
涉及4条主要说明.
How does it work?
There are 4 main instructions involved.
-
首先,我们创建一个组
DispatchGroup()
.该值将跟踪未决块的数量.
First of all we create a group
DispatchGroup()
. This value will keep track of the number of pending blocks.
let group = DispatchGroup()
然后之前,开始新的异步调用,我们告诉该组有一个新的未决块.
Then before starting a new asynchronous call we tell the group there is a new pending block.
group.enter()
在回调闭包中,我们告诉小组一个块已经完成了工作.
Inside the callback closure we tell the group that one block has finished its work.
group.leave()
当组中的块数确实变为零时,我们告诉该块运行闭包.
We tell the block to run a closure when the number of blocks into the group does become zero.
group.notify(queue: .main) {
print("All callbacks are completed")
}
这篇关于在所有Firebase调用完成后,如何重新加载数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!