swift firebase fetch followers uid然后获取他们的详细信息

时间:2017-10-18 11:26:29

标签: swift xcode

我想获取用户的关注者(在这种情况下,他们有一个关注者)然后我需要获取该关注者的详细信息以加载到一个单元格中以呈现在collectionviewcontroller中。

我是这样开始的:

fileprivate func fetchFollwersUID() {
    guard let uid = Auth.auth().currentUser?.uid else { return }
    Database.database().reference().child("Follwers").child(uid).observeSingleEvent(of: .childAdded, with: { (snapshot) in

快照是IfzJrhbtkjesmjHfFXgGcnjvUXO2:IfzJrhbtkjesmjHfFXgGcnjvUXO2

密钥和值都是密钥和值的folid的uid(仅适用于此示例)

我想将保存密钥作为密钥然后转到firebase.child(“用户”)并获取该用户的详细信息并将其附加到用户数组:[用户]

Database.database().reference().child("Users")    (search for user with "Key")

guard let dictionary = snapshot.value as? [String: Any] else { return }

let user = User(dictionary: dictionary)
self.users.append(user)
self.collectionView?.reloadData()

最后,我将附上一个用户详细信息,稍后我将在我的UICollectionViewCell中执行didSet并将其详细信息放入我的Contacts UICollectionViewController

1 个答案:

答案 0 :(得分:0)

如果您存储这样的用户信息:

"Users": {
    "UID1": {
        "Name": "Steve",
        "Email": "ScubaSteve@gmail.com"
    }
    "UID2": {
        ...
    }
}

您需要做的就是执行“嵌套”查询

fileprivate func fetchFollwersUID() { 
    guard let uid = Auth.auth().currentUser?.uid else { return }

    Database.database().reference().child("Follwers").child(uid).observeSingleEvent(of: .childAdded, with: { (snapshot) in {
        for child in snap.children {
            let child = child as? DataSnapshot
            if let key = child?.key {
                // query for that users information
                Database.database().reference.child("Users").child(key).observeSingleEvent(of: .value, with: { snap in 
                    guard let dictionary = snap.value as? [String: Any] else { return }
                    if let name = dictionary["Name"] as? String, let email = dictionary["Email"] as? String {
                        // do something with that user information
                    }
                })
            }
        }
    })