Query Firebase nested data to check specific value - iOS Swift

邮差的信 提交于 2019-12-24 19:04:44

问题


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"]
    }
   }

回答1:


When you perform a query, Firebase compares a property of each child node under the location you query to the value you pass in.

I assume that profilesRef points to /profiles in your database. In that case profilesRef.child(user) in your code points to the profile for a specific user already. When you then run a query, you are querying each of the properties of that specific user.

What you like want to do is to get all users with gender equal to Female. In that case you should query the user list:

DatabaseService.shared.profilesRef.queryOrdered(byChild: "gender").queryEqual(toValue: "Female")...

Note that you also should define the index on the level where the query is run, so on /users:

"profiles": {
  ".indexOn": ["gender"]
}

Another alternative (the might scale better) is to not use a query altogether, but instead load the gender property of each individual user in your array. In (approximate) code:

for user in nearbyUserArray {
  DatabaseService.shared.profilesRef.child(user).child( "gender").observeSingleEvent(of: .value) { [weak self] (snapshot) in
    if snapshot.value == "Female" {
      print("snap: \(snapshot)")
    }
  }
}


来源:https://stackoverflow.com/questions/47805142/query-firebase-nested-data-to-check-specific-value-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!