ios - Firebase ios : get list of users by IDs

标签 ios swift firebase firebase-realtime-database

情况:我有一个包含用户的数据库,用户可以有 friend ,并且 friend 作为 userID 链接到当前用户模型。因此,当我加载用户数据时,我还会收到他 friend 的 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 快照中检索这些用户的信息(即使用一个引用),而无需为每个 friend 创建新的引用,例如

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

最佳答案

使用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
                }
             })
           }
       }
    }
 })

关于ios - Firebase ios : get list of users by IDs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37611971/

相关文章:

android - 在 Ionic 中实现 Firebase 应用内消息

javascript - 将循环结构转换为 JSON Firebase

javascript - 范围错误 : Maximum call stack size exceeded in node_modules -> elasticsearch

ios - 如何给 UIImagePickerController 中的标题?

objective-c - 如何识别从左到右的滑动 Action ?

swift - 如何使用 Swift 在 mapkit 上更改未选择的注释图钉图像

ios - 使导航栏标题可快速点击

ios - 如何在 ios swift 中为事件设置自定义标识符?

ios - 自动布局在显示后更新 View

Swift 远程配置 : fetchAndActivate does not update local config