【问题标题】:Firebase ios: get list of users by IDsFirebase ios:按ID获取用户列表
【发布时间】:2016-06-03 10:28:23
【问题描述】:

情况:我有一个用户数据库,用户可以有朋友,朋友作为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")

【问题讨论】:

  • 请显示最小的 JSON(作为文本,没有屏幕截图),以显示您已经完成的操作以及您遇到的问题。没有它,就很难提供帮助。

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

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

【讨论】:

  • 我也在考虑同样的方法,但这是解决问题的好方法吗?我的意思是性能等怎么样?
猜你喜欢
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 2017-12-22
  • 2020-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多