I have an array of User ID Strings and I want to query data in Firebase Realtime Database to see if each User ID has a specific gender.
var nearbyUserArray = [String]()
Below is an example of the data I have in Firebase. The second level deep is the User ID (0UFhIgGAwIY1btnBeNaNkJruYKJ3). What I am trying to do is loop through my User ID array and for each User ID check if that specific user is Male. If the user is Female, remove that User ID from the array.
"profiles" : {
"0UFhIgGAwIY1btnBeNaNkJruYKJ3" : {
"dateOfBirth" : "May 11, 1987",
"gender" : "Female",
"higherAgeRange" : 36,
"interestedIn" : "Male",
"lowerAgeRange" : 29,
"name" : "Sally"
},
"6vNjngZqoTbd4oGudip3NX78pu32" : {
"dateOfBirth" : "December 23, 1990",
"gender" : "Male",
"higherAgeRange" : 39,
"interestedIn" : "Female",
"lowerAgeRange" : 25,
"name" : "Bob"
}
How would you recommend I accomplish this? I want to minimize the amount of data I have to download and manipulate on the client side. Below is what I am attempting to do but I cannot get it to work. Any ideas why this always returns Null?
func getUsersWithinGenderCriteria() {
for user in nearbyUserArray {
DatabaseService.shared.profilesRef.child(user).queryOrdered(byChild: "gender").queryEqual(toValue: "Female").observeSingleEvent(of: .value) { [weak self] (snapshot) in
print("snap: \(snapshot)")
// The idea is if snapshot is Female, remove the user ID from the array. If the snapshot is Null, keep it in the array. However, this is not working as all snapshots come back as null.
}
}
}
Also, here are the security rules I set up.
"profiles": {
"$userId": {
".indexOn": ["gender"]
}
}