Firebase 数据库中的数据是异步读取的。这意味着您的代码执行顺序可能不是您所期望的。最简单的方法是添加一些打印语句:
func fetchUidWithEmail (email: String) -> String {
print("Before starting query")
ref.child("userList").queryOrderedByChild("email").queryEqualToValue(email).observeEventType(.Value, withBlock: { (snapshot) in
print("In query callback block")
})
print("After starting query")
}
当你运行这段代码时,输出是:
开始查询之前
开始查询后
在查询回调块中
这可能不是您所期望的。但它确实解释了为什么返回 uid 不起作用:当你返回 uid 时,它还没有从 Firebase 加载。
解决这个问题的方法是改变你对问题的看法。与其说“先获取 uid,然后用 uid 做某事”,不如尝试将其表述为“每当我们得到 uid 时,就用它做一些事情”。
最简单的方法是将需要 uid 的代码写入加载数据的函数中:
func fetchUidWithEmail (email: String) -> String {
ref.child("userList").queryOrderedByChild("email").queryEqualToValue(email).observeEventType(.Value, withBlock: { (snapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
print(snap.key)
// TODO: do anything you want with the uid here
}
}
})
}
虽然这可行,但这意味着您可能有很多地方可以加载 uid-by-email。如果您可以将需要 uid 的代码传递给您的函数,则更可重用:
func fetchUidWithEmail (email: String, withBlock: String -> Void) -> Void {
ref.child("userList").queryOrderedByChild("email").queryEqualToValue(email).observeEventType(.Value, withBlock: { (snapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
withBlock(snap.key)
}
}
})
}
然后你可以调用它:
fetchUidWithEmail("frank.vanpuffelen@gmail.com") { (uid) in
print(uid)
}
这正是 Firebase 数据库客户端本身的工作方式:添加观察者时,会传入回调/块。