使用 Firebase 异步编写代码 [英] Make code with Firebase asynchronous
问题描述
我正在尝试创建一个算法,该算法创建 12 个结构对象(问题)并将它们放入一个数组(问题)中.但是,它似乎不起作用,因为在来自 Firebase 的数据能够修改它们之前创建了对象.我试图让它们异步,但我在网上发现的一切都不起作用.提前致谢.
I am trying to create an algorithm that creates 12 struct objects(question) and puts them in an array(questions). However, it doesn't seem to work as the objects are created before the data from Firebase has been able to modify them. I was trying to make them asynchronous but nothing that I found online worked. Thanks in advance.
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("NumberOfQuestions").observeSingleEvent(of: .value, with: { snapshot in
while self.questions.count < 12{
var question = questionMGR() //questionMGR is the name of my struct
let questionNumber = String(Int(arc4random_uniform(snapshot.value as! UInt32) + 1))
databaseRef.child("questions").child("questionNumber: " + questionNum).child("title").observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.value ?? "")
question.title = (snapshot.value as? String)!
databaseRef.child("questions").child("questionNumber: " + questionNum).child("description").observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.value ?? "")
question.desc = (snapshot.value as? String)!
})
})
self.questions.append(question)
}
})
推荐答案
遇到这种情况,通常的解决办法是将操作移到里面回调:
When this happens, the solution is usually to move the operation inside the callback:
databaseRef.child("questions").child("questionNumber: " + questionNum).child("title").observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.value ?? "")
question.title = (snapshot.value as? String)!
databaseRef.child("questions").childSnapshot(forPath: "questionNumber: " + questionNum).childSnapshot(forPath: "description").observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.value ?? "")
question.desc = (snapshot.value as? String)!
self.questions.append(question)
})
})
但在这种情况下,我想知道您为什么不简单地一次性加载整个问题:
But in this case I wonder why you don't simply load the entire question in one go:
FIRDatabaseReference qref = databaseRef.child("questions").child("questionNumber: " + questionNum)
qref.observeSingleEvent(of: .value, with: { snapshot in
var question = questionMGR()
question.title = (snapshot.childSnapshot(forPath: "title").value as? String)!
question.desc = (snapshot.childSnapshot(forPath: "description").value as? String)!
self.questions.append(question)
})
我会考虑对标题和描述进行两次单独调用的唯一原因是,如果您的问题包含更多您在此处不需要的数据.但如果是这样的话,我会改造你的 JSON 以将标题和描述分开到一个单独的顶级列表中.
The only reason I'd ever consider two separate calls for title and description is if your questions contain a lot more data that you don't need here. But if that's the case, I'd remodel your JSON to separate the title and description into a separate top-level list.
在 NoSQL 中,将数据建模为与屏幕上显示的非常相似通常是一件好事 (tm).因此,如果您有每个问题的标题和描述的列表,您应该考虑准确存储:每个问题的标题和描述的列表.
In NoSQL it's often a Good Thing (tm) to model your data to be very similar to what you show on the screen. So if you have a list if the title and description of each question, you should consider storing precisely that: a list of the the title and description for each question.
这篇关于使用 Firebase 异步编写代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!