Firebase RTDB:如何根据嵌套字段过滤子事件?
Firebase RTDB: How to filter child events based on a nested field?
鉴于我的 Firebase 实时数据库中的每个条目都有一个 version
字段表示上次编辑的时间戳,我如何按版本过滤它们,以便只有比 X 更新的子事件送达了吗?
val listener = object : ChildEventListener {
// Not relevant
}
val ref = database.getReference("exercises").child("userId")
ref.addChildEventListener(listener)
// TODO: ref.filter(version>localVersion)
有关我为什么要这样做的上下文。条目非常多,虽然我想知道它们何时发生变化,但始终观察所有条目会导致内存占用过大(>150 mb)。
Json 数据模型的表示(练习):
{
"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.
}
- 如何按版本过滤它们,以便只传送比 X 更新的子事件?
由于您的 version
字段是一个数字,要仅获取比特定值更新的对象,您可以使用 Query#startAt() 函数,其中:
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.
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.
鉴于我的 Firebase 实时数据库中的每个条目都有一个 version
字段表示上次编辑的时间戳,我如何按版本过滤它们,以便只有比 X 更新的子事件送达了吗?
val listener = object : ChildEventListener {
// Not relevant
}
val ref = database.getReference("exercises").child("userId")
ref.addChildEventListener(listener)
// TODO: ref.filter(version>localVersion)
有关我为什么要这样做的上下文。条目非常多,虽然我想知道它们何时发生变化,但始终观察所有条目会导致内存占用过大(>150 mb)。
Json 数据模型的表示(练习):
{
"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.
}
- 如何按版本过滤它们,以便只传送比 X 更新的子事件?
由于您的 version
字段是一个数字,要仅获取比特定值更新的对象,您可以使用 Query#startAt() 函数,其中:
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.
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.