【发布时间】:2018-01-15 13:16:22
【问题描述】:
我想使用DisaptchGroup 在迭代后调用func 并将元素附加到数组中。
这是firebase节点的样子
{
"tags": {
"NewYork": {
uid1: 1
uid2: 1
},
"baseball": {
uid1: 1
}
}
}
所以,基本上我所做的是在用户选择标签之后,获取标签下的 uid 并将这些 uid 传递给数组。
func filterTempo(filterTag: FilteredTags) {
let group = DispatchGroup()
let tag = filterTag.selectedTag.components(separatedBy: "/") //it reutrns "NewYork" and "baseball"
tag.forEach { (key) in
self.ref.child("tags").child(key).observe(.value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String:Any] else { return }
for (key,_) in dictionary {
self.arrays.append(key)
}
// want call func after iteration has finished.
}, withCancel: nil)
}
}//func
第一次迭代后,数组将像这样[uid1, uid2]。然后在第二次迭代之后,数组将看起来像这样[uid1, uid2, uid1]。我想在迭代完成而不是中途之后调用函数(func fetchTeams(array: [String]))。为了实现这一点,我尝试使用DispatchGroup,但我在func fetchTeams 中尝试了print(array),它会像这样返回。
[uid1, uid2]
[uid1, uid2, uid1]
我应该在哪里打电话给DispatchGroup.enter() 和DispatchGroup.leave() 来解决这个问题?
提前谢谢!
【问题讨论】:
标签: arrays swift firebase-realtime-database dispatch dispatch-async