在Firebase中,我有一个"用户"每个用户在其UID下存储的值(名称,头像等)的节点:
"users" : {
"eMKRoygZUJZ9VFMHzzO15tnrtYf2" : {
"gender" : "female",
"handle" : "sav",
"name" : "Savina",
"profilePicture" : "https://graph.facebook.com/*removed*/picture?type=large&return_ssl_resources=1",
}
现在我需要获取特定用户集的信息 - 我有一组我想要获取(returnedParticipants
)数据的所有用户的UID。目前,下面的函数返回应用程序中所有用户的值 - 我想知道如何过滤此请求,因此我只查询其UID在returnedParticipants
数组中的用户,因此我可以设置它们的值相应的一些词典:
var returnedParticipants = [String]()
// [UID : value]
var avatarDictionary: [String : AnyObject] = [:]
var nameDictionary: [String : AnyObject] = [:]
var genderDictionary: [String : AnyObject] = [:]
func getParticipantInfo() {
// Participant UIDs
guard let participantUIDs = self.returnedParticipants as? [String] else {print("Could not get room's participants"); return}
let databaseRef = FIRDatabase.database().reference()
// Query Firebase users with those UIDs & grab their gender, profilePicture, handle, and name
databaseRef.child("users").observeSingleEvent(of: .value, with: { (snapshot) in
if let snapDict = snapshot.value as? [String:AnyObject]{
for each in snapDict {
let keys = each.key
let avatars = each.value["profilePicture"] as! String
let genders = each.value["gender"] as! String
let handles = each.value["handle"] as! String
let names = each.value["name"] as! String
print("\n\nUID: \(keys)")
print("Avatar: \(avatars)")
print("Gender: \(genders)")
print("Handle: \(handles)")
print("Name: \(names)")
}
}
}, withCancel: {(Err) in
print(Err.localizedDescription)
})
// Set those to the dictionaries [UID : value]
}
我搜索并在SO Use Firebase-query to get data from an array of ID's上看到了这个问题,但我并不熟悉那种语言。我查看了文档的这一页:https://firebase.google.com/docs/database/ios/lists-of-data,但无法找到关于为密钥列表执行查询的任何内容。
如果我想一次搜索一个特定的UID,我可以使用queryOrdered(byChild: *someUID*)
,但这需要多次调用Firebase。
是否有一种有效的方法可以在一个调用/一个函数中完成所有操作,而不是为每个UID单独进行观察?也许从数组或UID使用queryEqualToValue
或queryStarting / Ending值的方法?
完整数据库结构:
{
"rooms" : {
"-Ki6fAHv6LS5pyx34nUr" : {
"messages" : {
"-Ki70oJOAsto1uIxJsLY" : {
"senderId" : "tzfHgGKWLEPzPU9GvkO4XE1QKy53",
"senderName" : "Timothy",
"text" : "Test"
}
},
"participants" : {
"tzfHgGKWLEPzPU9GvkO4XE1QKy53" : true
},
"roomName" : "Room One"
},
"-Ki6fDsTUTdNAw8elnP9" : {
"participants" : {
"tzfHgGKWLEPzPU9GvkO4XE1QKy53" : true
},
"roomName" : "Room Two"
},
"-KiBBeexijDJri_w7fye" : {
"participants" : {
"eMKRoygZUJZ9VFMHzzO15tnrtYf2" : true
},
"roomName" : "Room Three"
}
},
"users" : {
"eMKRoygZUJZ9VFMHzzO15tnrtYf2" : {
"gender" : "female",
"handle" : "sav",
"name" : "Savina",
"profilePicture" : "https://graph.facebook.com/1929991317219760/picture?type=large&return_ssl_resources=1"
},
"tzfHgGKWLEPzPU9GvkO4XE1QKy53" : {
"gender" : "male",
"handle" : "TestHandleOne",
"name" : "Timothy",
"profilePicture" : "https://graph.facebook.com/1682119565138679/picture?type=large&return_ssl_resources=1"
}
}
}