首页 文章

从当前登录的用户Firebase中检索数据

提问于
浏览
0

我已经尝试了许多解决方案来完成这项工作,但它似乎并不适用于我 . 这些是我尝试过的解决方案:

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

My database

我正在尝试检索当前登录用户的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!)

})

1 回答

  • 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)
    }
    

相关问题