只有在从Firebase下载数据后才能执行操作 [英] How to perform an action only after data are downloaded from Firebase
问题描述
我试图找出如何触发火力地堡的observeSingleEvent后才能完成其工作的作用。
背后的原因是,我需要做的以下操作:
- 据我需要从数据库中获取一些数据的用户一个输入
- 当这些数据被完全下载,我需要处理它们一点点,用于检索其它数据被显示给用户
$ p $ lt; code> let listOfTags = [String]()
var outputArray = [string]()
如果listOfTags.count!= 0 {
为listOfTags中的标签{
ref.child(tags)。child(tag ).observeSingleEvent(:.value,其中:{(snapshot)in
if temp = snapshot.children.allObjects as?[FIRDataSnapshot] {
for temp in {
outputArray.append(elem.k EY)
}
}
// ...
}){(错误)在
打印(error.localizedDescription)
}
}
//这里我想在循环完成后执行一个操作
//完成并且outputArray被所有的值赋值
doSomethingHere()
}
但是我注意到函数doSomethingHere ()在outputArray仍然是EMPTY时立即执行!所以它什么也没做。然后我的问题是,我怎样才能构造程序,使得这个函数在下载过程完成后执行?也许observeSingleEvent是不是正确的Firebase方法使用?对于这个特定的情况,我不想观察任何改变(因为这个列表并没有经常改变),我只需要下载数据,稍后用这些数据做一些其他的事情。
Firebase函数中的闭包就是这样做的 - 当数据(快照)有效时,闭包内的代码将执行。
移动doSomethingHere到火力地堡闭包内,它会以正确的顺序执行 - 在这种情况下
如果listOfTags.count = 0 {$!
$ b ref.child(tags)。child(tag).observeSingleEvent(of:.value,with:{(snapshot)in
如果让temp = snapshot.children.allObjects as?[FIRDataSnapshot] {
为临时元素{
outputArray.append(elem。 key)
}
doSomethingHere()//在此处填充数组
}
})
}
}
请注意,您也可以删除这个
如果listOfTags.count!= 0
好像listOfTags是0,for循环将不会执行。
I'm trying to figure out how to trigger an action only after the observeSingleEvent in Firebase has finished its work.
The reason behind is that I need to do the following operations:
- According to one input from the user I need to get some data from the database
- When these data are completely downloaded, I need to process them a little bit and use to retrieve other data to be shown to the user
Currently I'm using this code:
let listOfTags = [String]()
var outputArray = [String]()
if listOfTags.count != 0 {
for tag in listOfTags {
ref.child("tags").child(tag).observeSingleEvent(of: .value, with: { (snapshot) in
if let temp = snapshot.children.allObjects as? [FIRDataSnapshot] {
for elem in temp {
outputArray.append(elem.key)
}
}
// ...
}) { (error) in
print(error.localizedDescription)
}
}
// HERE I WANT TO DO AN ACTION "ONLY" AFTER THE FOR LOOP IS
// COMPLETED AND THE "outputArray" IS FILLED WITH ALL VALUES
doSomethingHere()
}
But I noticed that the function doSomethingHere() is executed immediately when the outputArray is still EMPTY! So it does nothing.
Then my question is, how can I structure the program such that this function is executed after the downloading process is completed? Maybe observeSingleEvent is not the correct Firebase method to be used? For this specific case I don't want to observe any change (because this list is not changing so often), I just need to download data and do something else with these data later ..
Firebase is asynchronous so to make code execute in a defined order, you need to 'wait' for firebase to return data.
The closures that go with Firebase functions do just that - the code within the closure executes when the data (snapshot) is valid.
Move doSomethingHere() to within the Firebase closure and it will execute in the correct order - in this case
if listOfTags.count != 0 {
for tag in listOfTags {
ref.child("tags").child(tag).observeSingleEvent(of: .value, with: { (snapshot) in
if let temp = snapshot.children.allObjects as? [FIRDataSnapshot] {
for elem in temp {
outputArray.append(elem.key)
}
doSomethingHere() //array is populated at this point
}
})
}
}
Note you can also remove this
if listOfTags.count != 0
as if listOfTags is 0, the for loop will not execute.
这篇关于只有在从Firebase下载数据后才能执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!