小编Spa*_*ark的帖子

如何从Firebase同步检索数据?

我有两个集合,即用户和问题.

根据使用userId登录的用户,我currQuestionusers集合中检索值.

根据该currQuestion值,我需要question从Firebase Questions集合中检索文档.

我使用下面的代码来检索userId

rootRef.child("0").child("users")
        .queryOrderedByChild("userId")
        .queryEqualToValue("578ab1a0e9c2389b23a0e870")
        .observeSingleEventOfType(.Value, withBlock: { (snapshot) in

            for child in snapshot.children {
                self.currQuestion = child.value["currentQuestion"] as! Int
            }
            print("Current Question is \(self.currQuestion)")

            //print(snapshot.value as! Array<AnyObject>)
        }, withCancelBlock : { error in
                print(error.description)
        })
Run Code Online (Sandbox Code Playgroud)

并检索问题

rootRef.child("0").child("questions")
.queryOrderedByChild("id")
.queryEqualToValue(currQuestion)
.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
            for child in snapshot.children {
                print(child.value["question"] as! String)
            }

            }, withCancelBlock: { error in
                print(error.description)
        })
Run Code Online (Sandbox Code Playgroud)

但是上面的代码是异步执行的.我需要解决方案使这个同步或如何实现监听器,以便我可以在currQuestion值更改后重新启动问题查询?

events ios swift firebase-realtime-database

1
推荐指数
1
解决办法
3206
查看次数

标签 统计

events ×1

firebase-realtime-database ×1

ios ×1

swift ×1