使用Firebase异步编写代码 [英] Make code with Firebase asynchronous

查看:99
本文介绍了使用Firebase异步编写代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一种算法,该算法创建12个struct对象(问题)并将其放入数组(问题).但是,由于对象是在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆