我正在使用Firebase数据库处理Swift应用。我有这样的结构:
value:
我试图访问成员中的数据,以查看用户是否在该组中。
我之前使用的是"Groups" : {
"-KWewQiWc6QPfiq5X1AY" : {
"Members" : [ "emmaturner", "maxturner", "ethanturner" ],
"Teacher" : "ethanturner",
"Title" : "Kimbra Followers",
"groupId" : "-KWewQiWc6QPfiq5X1AY"
}
}
但是,我了解到,如果用户是教师,这只会返回该组。我尝试使用ref.child("Groups").queryOrdered(byChild: "Teacher").queryEqual(toValue: userId)
和"members/"
代替教师,但当然这些要么不起作用,要么只返回第一个用户的访问权限。只要组成员数组中提到了他们的名字,我怎样才能让Firebase返回该组?
答案 0 :(得分:3)
试试这个:
var ref: FIRDatabaseReference!
ref = FIRDatabase.database().reference()
var membersArray = [String]()
ref.child("Groups").observeSingleEvent(of: .value, with: { (snapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots
{
let teacher = snap.childSnapshot(forPath: "Teacher").value as! String
self.ref.child("Groups").child(snap.key).child("Members").observeSingleEvent(of: .value, with: { (member) in
if let members = member.children.allObjects as? [FIRDataSnapshot] {
for mem in members
{
print(mem.value as! String)
let currentMember = mem.value as! String
membersArray.append(currentMember)
}
}
if(membersArray.contains(teacher))
{
print(membersArray)
}
})
}
}
})