我已经尝试了许多解决方案来实现此目的,但似乎不适用于我。这些是我尝试过的解决方案:
Firebase Retrieving Data in Swift
https://firebase.google.com/docs/database/ios/read-and-write
https://www.raywenderlich.com/187417/firebase-tutorial-getting-started-3
我正在尝试检索当前登录用户的deviceToken,例如,如果John登录,它将分配或检索他的deviceToken并将其分配给变量。
我得到的与此代码最接近的代码,但是我得到的每个配置文件都具有存储在该配置文件中的所有数据,而不是当前登录的配置文件。
let userID = Auth.auth().currentUser?.uid
let ref = Database.database().reference(withPath: "users/profile/\(userID)")
ref.observeSingleEvent(of: .value, with: { snapshot in
if !snapshot.exists() {
return
}
let token = snapshot.childSnapshot(forPath: "deviceToken").value
print(token!)
})
答案 0 :(得分:1)
文档中有一个示例,针对您的情况进行了修改:
let userID = Auth.auth().currentUser?.uid
ref.child("users").child("profile").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let deviceToken = value?["deviceToken"] as? Int ?? ""
// ...
}) { (error) in
print(error.localizedDescription)
}