首页 文章

Firebase ios:按ID获取用户列表

提问于
浏览
0

情况:我有一个用户数据库,用户可以将朋友和朋友链接到当前用户模型作为userID . 因此,当我正在加载用户数据时,我还收到了他的朋友的firebase ID列表 .

问题:有没有办法一次性收到所有这些用户的firebase快照?没有找到任何合适的解决方案 - .child()直接链接到一个对象,queryOrderedByChild()和queryOrderedByValue()似乎没有提出这样的请求 .

将不胜感激任何建议 .

编辑:数据库结构:

{
    "Users": {
        "user_id_first" : {
            "user_info" : {
                "name": "name",
                "age": "age"
            },
            "friends": {}
        },

        "user_id_second" : {
            "user_info" : {
                "name": "name",
                "age": "age"
            },
            "friends": {}
        },

        "user_id_third" : {
            "user_info" : {
                "name": "name",
                "age": "age"
            },
            "friends": {
                "user_id_first" : true,
                "user_id_second" : true
            }
        }
    }
}

有朋友ID列表,实际上是firebase用户的ID . 我只需要在一个firebase快照中检索这些用户的信息(即使用一个引用),而不为每个朋友创建新的引用

myDBRef.child("Users").child("friend_id")

1 回答

  • 0

    使用for循环访问所有的friendsId: -

    let parentRef = FIRDatabase.database().reference().child("Users")
       parentRef.child(FIRAuth.auth()!.currentUser!.uid).child("friends").observeEventType(.Value, withBlock: {(snapshot) in
    
      if snapshot.exists(){
    
        if let friendsDictionary = snapshot.Value as! NSMutableDictionary{
    
                  for each in friendsDictionary as! [String : AnyObject]{
    
                         let friendsId = each.0 as! String
    
                         parentRef.child(friendId).observeEventType(.Value, withBlock: {(friendDictionary)
    
                         if let friendsInfo = friendDictionary.Value as! NSMutableDictionary{
                              //retrieve the info
                    }
                 })
               }
           }
        }
     })
    

相关问题