Spa*_*ark 1 events ios swift firebase-realtime-database
我有两个集合,即用户和问题.
根据使用userId登录的用户,我currQuestion从users集合中检索值.
根据该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值更改后重新启动问题查询?
编写自己的方法,将完成处理程序作为参数,并等待该代码块完成.像这样:
func someMethod(completion: (Bool) -> ()){
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)")
completion(true)
//print(snapshot.value as! Array<AnyObject>)
}, withCancelBlock : { error in
print(error.description)
})
}
Run Code Online (Sandbox Code Playgroud)
然后,只要你想调用该函数,就这样调用:
someMethod{ success in
if success{
//Here currValue is updated. Do what you want.
}
else{
//It is not updated and some error occurred. Do what you want.
}
}
Run Code Online (Sandbox Code Playgroud)
完成处理程序通常用于等待一段代码完全执行.PS只要它们不阻塞主线程,异步请求就会通过添加完成处理程序来实现同步,就像上面显示的代码一样.
它只是等待你currValue首先更新(async从服务器接收数据)然后当你someMethod像我显示的那样调用时,因为函数的最后一个参数someMethod是一个闭包(又名,尾随Closure)),你可以跳过括号并调用它.这是一个关于闭包的好读物.因为闭包是类型(Bool) - >(),所以你只需告诉你someMethod什么时候完成任务就像completion(true)在我的代码中一样,然后在调用它时,你可以调用它success(你可以使用你想要的任何单词) )它将是Bool类型,因为它是这样声明的,然后在函数调用中使用它.希望能帮助到你.:)
| 归档时间: |
|
| 查看次数: |
3206 次 |
| 最近记录: |