查询Firebase嵌套数据以检查特定值-iOS Swift [英] Query Firebase nested data to check specific value - iOS Swift

查看:33
本文介绍了查询Firebase嵌套数据以检查特定值-iOS Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户ID字符串数组,我想查询Firebase实时数据库中的数据,以查看每个用户ID是否具有特定的性别.

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]()

以下是我在Firebase中拥有的数据的示例.第二层是用户ID(0UFhIgGAwIY1btnBeNaNKJruYKJ3).我想做的是遍历我的用户ID数组,并针对每个用户ID检查该特定用户是否为Male.如果用户是女性,请从阵列中删除该用户ID.

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

您如何建议我完成此任务?我想减少必须在客户端下载和处理的数据量.以下是我要尝试执行的操作,但无法正常工作.为什么这总是返回Null的任何想法?

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

推荐答案

执行查询时,Firebase会将查询位置下的每个子节点的属性与您传递的值进行比较.

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

我假定profilesRef指向数据库中的/profiles.在这种情况下,代码中的profilesRef.child(user)已经指向特定用户的配置文件.然后,当您运行查询时,您正在查询该特定用户的每个属性.

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.

您想要做的是使所有用户的gender等于Female.在这种情况下,您应该查询用户列表:

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")...

请注意,您还应该在运行查询的级别上定义索引,例如/users:

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

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

另一种选择(可能会更好地扩展)是不完全使用查询,而是加载数组中每个用户的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)")
    }
  }
}

这篇关于查询Firebase嵌套数据以检查特定值-iOS Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆