【发布时间】:2016-07-17 02:16:46
【问题描述】:
我有两个集合,即用户和问题。
基于使用 userId 登录的用户,我从 users 集合中检索 currQuestion 值。
基于currQuestion 值,我需要从Firebase Questions 集合中检索question 文档。
我使用下面的代码来检索 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)
})
并检索问题
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)
})
但是上面的代码是异步执行的。我需要解决方案以使此同步或如何实现侦听器,以便在更改 currQuestion 值后回火问题查询?
【问题讨论】:
-
使用完成块做任何你需要的事情。不要尝试同步写入
标签: ios swift events firebase-realtime-database