【问题标题】:Filter Firebase query with array?使用数组过滤 Firebase 查询?
【发布时间】:2017-10-11 23:41:40
【问题描述】:

我正在创建一个简单的 Tinder 克隆来学习 Firebase,但我不确定如何向此 Firebase 查询添加过滤器(我正在使用 GeoFire 在半径范围内进行搜索):

//get closest user for next game within radius
let center = CLLocation(latitude: latitude, longitude: longitude)
var circleQuery = geoFire.query(at: center, withRadius: 149.0)

var queryHandle = circleQuery?.observe(.keyEntered, with: { (key: String!, location: CLLocation!) in
    print("Key '\(key)' entered the search area and is at location '\(location)'")

我需要过滤掉当前用户已经接受/拒绝的用户。我可以为当前用户添加一个包含接受/拒绝用户的数组,但我担心每次在查询中返回一个孩子时迭代一个数组可能会消耗大量数据。

【问题讨论】:

  • 没有简单的方法可以向 GeoFire 查询添加附加条件。您必须在客户端代码中过滤附加条件。

标签: swift firebase firebase-realtime-database geofire


【解决方案1】:

您需要拥有匹配/拒绝的数组。您可能会在用户对象中存储一个数组以进行拒绝和匹配,但您需要使用 firebase 安全规则,以便您可以选择性地向具有匹配列表的用户公开哪些数据/rejected 用户 ID。

由于接受的列表存储在两个用户的用户对象中,您最终还会得到一些重复的数据,但这是 firebase 的本质。这样做不会导致任何性能挂断或任何事情。

如果您按照我在此处建议的方式进行操作,则在创建要滑动的用户卡列表时,您只需使用通配符(Firebase 在技术上不支持通配符,但他们的文档解释了一种解决方法this) 用于用户 ID,然后设置在潜在匹配被拒绝数组中找不到当前用户 ID 的位置。我已将有关过滤的文档链接起来作为参考。

https://firebase.google.com/docs/database/ios/lists-of-data#filtering_data

【讨论】:

    猜你喜欢
    • 2018-08-04
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    相关资源
    最近更新 更多