简体   繁体   English

Firebase RTDB:如何根据嵌套字段过滤子事件?

[英]Firebase RTDB: How to filter child events based on a nested field?

Given that every entry in my Firebase Realtime Database has a version field that represents the timestamp it was last edited, how can I filter them by their version such that only child events that are newer than X are delivered?鉴于我的 Firebase 实时数据库中的每个条目都有一个version字段,表示它上次编辑的时间戳,我如何按版本过滤它们,以便只传递比 X 更新的子事件?

val listener = object : ChildEventListener {
    // Not relevant
}

val ref = database.getReference("exercises").child("userId")
ref.addChildEventListener(listener)

// TODO: ref.filter(version>localVersion)

For context as to why Im looking to do this.关于我为什么要这样做的背景。 There are very many entries, and while Id like to know whenever any of them change, observing all of them at all times results in too big of a memory footprint (>150 mb).有很多条目,虽然我想知道它们何时发生变化,但始终观察所有条目会导致 memory 占用空间太大(> 150 mb)。

Json representation of a data model (exercise): Json 数据 model 的表示(练习):

{
  "details" : {
    "category" : "LIFT",
    "equipment" : "CABLE_MACHINE",
    "muscles" : [ "BACK" ],
    "name" : "Row in Cable Machine",
    "status" : "AVAILABLE"
  },
  "id" : "1",
  "source" : "CUSTOM",
  "version" : 1648446264560 // This is the field Id like to filter on.
}
  • How can I filter them by their version such that only child events that are newer than X are delivered?我如何按版本过滤它们,以便只传递比 X 更新的子事件?

Since your version filed is a number, to get only the objects newer than a particular value you can use the Query#startAt() function, which:由于您提交的version是一个数字,要仅获取比特定值更新的对象,您可以使用Query#startAt() function,其中:

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.使用给定的 orderBy 指令或优先级作为默认值,创建一个限制为仅返回值大于或等于给定值的子节点的查询。

Or Query#startAfter() function that:或者Query#startAfter() function 那:

Creates a query constrained to only return child nodes with a value greater than the given value, using the given orderBy directive or priority as default.使用给定的 orderBy 指令或优先级作为默认值,创建一个限制为仅返回值大于给定值的子节点的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM