仅从Firebase检索当前用户的子值

时间:2017-04-07 13:43:42

标签: ios json firebase swift3 firebase-realtime-database

编辑,在底部更新,仍有问题

我将我的用户存储在Firebase中 - 每个用户的值存储在他们的uid(FIRAuth.auth()?.currentUser?.uid)下:

"users" : {
    "tzfHgGKWLEPzPU9GvkO4XE1QKy53" : {
      "gender" : "male",
      "handle" : "TestHandle2",
      "name" : "Timothy",
      "profilePicture" : "https://graph.facebook.com/*removed*/picture?type=large&return_ssl_resources=1"
    }
  }

现在要获取用户的信息(将其设置为我的User对象以在应用程序周围使用),我有这个功能:

func getUserInfo() {
    let userRef: FIRDatabaseReference = FIRDatabase.database().reference().child("users")

    // How to get specific user (current user only)??
    userRef.observeSingleEvent(of: .value, with: { (snapshot) in
        let users = snapshot.value as! [String: Dictionary<String, String>]
        for (key, value) in users {
            guard let name = value["name"] else {print("Error getting user name"); return}
            guard let handle = value["handle"] else {print("Error getting user handle"); return}
            guard let gender = value["gender"] else {print("Error getting user gender"); return}
            guard let profilePicture = value["profilePicture"] else {print("Error getting user profilePicture"); return}
            print("\n\nName: \(name)\nHandle: \(handle)\nGender: \(gender)\nProfile Picture: \(profilePicture)\n\n")
        }
    })
}

但是这会返回数据库中所有用户的值 - 我只想获取当前用户的值。

我可以添加/更改此功能以确保它只检索当前用户的值,而不是所有用户的值?另外(如果有必要,我可以单独提出一个问题,但这似乎与此相关),这种结构是将每个用户的值存储为UID的子项,这是一个很好的方法吗?我注意到在其他示例中很多时候它们都存储在childByAutoId下。我不确定会有什么好处。

感谢您的帮助!

编辑:

如果我将另一个子(uid)添加到引用的末尾然后转换为[String : AnyObject]字典,我可以获取当前用户的值,但解析它似乎是一个问题。

func getUserInfo() {

    guard let uid = FIRAuth.auth()?.currentUser?.uid else {print("Error getting user UID"); return}
    let userRef: FIRDatabaseReference = FIRDatabase.database().reference().child("users").child(uid)
    self.currentUser?.uid = uid

    userRef.observeSingleEvent(of: .value, with: { (snapshot) in

        if let usersDict = snapshot.value as? [String : AnyObject] {
            print(usersDict)
            for user in usersDict {
//                    guard let name = user.value(forKey: "name") else {print("Error getting user name"); return}
//                    guard let handle = user.value(forKey: "handle") else {print("Error getting user handle"); return}
//                    guard let gender = user.value(forKey: "gender") else {print("Error getting user gender"); return}
//                    guard let profilePicture = user.value(forKey: "profilePicture") else {print("Error getting user profilePicture"); return}
//                    print("\n\nName: \(name)\nHandle: \(handle)\nGender: \(gender)\nProfile Picture: \(profilePicture)\n\n")
            }
        }
    })
}

我在每个user.value(forKey)行上都出错,说明&#34;不能调用非函数类型AnyObject&#34;的值。

如果我在循环中print(user.value),我会完全恢复所有当前用户的值。我不能单独指定它们。

4 个答案:

答案 0 :(得分:0)

比你想象的容易: - )

    let userRef: FIRDatabaseReference = FIRDatabase.database().reference().child("users").child(FIRAuth.auth()?.currentUser?.uid)

答案 1 :(得分:0)

您需要向userRef

添加另一个孩子
guard let uid = FIRAuth.auth()?.currentUser?.uid else {
        return
    }

let userRef: FIRDatabaseReference = FIRDatabase.database().reference().child("users").child(uid)

答案 2 :(得分:0)

首先获取当前用户的UID,

let currentUid = FIRAuth.auth()?.currentUser?.uid 

然后替换此数据库引用,

let userRef: FIRDatabaseReference = FIRDatabase.database().reference().child("users")

有了这个,

let userRef: FIRDatabaseReference = FIRDatabase.database().reference().child("users").child(currentUid)

: - )

答案 3 :(得分:0)

以下是我如何加载用户信息

首先,我创建了一个名为User ...的NSObject类

 import UIKit

class User: NSObject {

// Add Whatever Child values of the user you have here
var email: String?
var nickname: String?

}

然后我打电话给

func GetUserInfo() {

guard let uid = FIRAuth.auth()?.currentUser?.uid else {
        return
    }

FIRDatabase.database().reference().child("users").child(uid)
.observeSingleEvent(of: .value, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {

                let user = User()
                user.setValuesForKeys(dictionary)
                print(user.nickname!)

    }, withCancel: nil)
}

不知道这是否有帮助。