情况:我有一个包含用户的数据库,用户可以有 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/