使用firebase过滤的分页列表

时间:2016-12-09 17:37:23

标签: swift firebase firebase-realtime-database

我尝试使用firebase和swift创建一个分页过滤列表(但可以随意使用您最喜欢的编程语言来回答)而不过滤客户端上检索到的数据。

让我们假设我有这个结构

matches
  match-1
    name: "Match 1"
    users
        user-1: "ok"
        user-2: true
  match-2
    name: "Match 2"
    users
        user-1: "ok"
        user-2: true
        user-3: true
  match-3
    name: "Match 3"
    users
        user-1: true
        user-2: true
        user-3: true
...

现在我想得到一个分页列表,列出了用户1的所有匹配值#34; ok"

我正在做这样的事情

matchesRef
        .queryOrdered(byChild: "users/user-1")
        .queryEqual(toValue: "ok")
        .queryStarting(atValue: "<last-fetched-user-id>")
        .queryLimited(toFirst: 5)
        .observe(.value, with: { snapshot in

});

但它会导致崩溃,因为&#34;无法调用queryStartingAtValue:之后调用queryStartingAtValue或queryEqualToValue&#34;

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

如果我没记错的话,你可以传递第一项的作为第二个参数返回queryStarting。来自documentation(强调我的):

  

queryStartingAtValue:childKey:用于生成对此位置数据的有限视图的引用。 FIRDatabaseQuery返回的queryStartingAtValue:childKey实例将响应值大于startValue的节点上的事件,或等于startValue且键大于或等于childKey 。 / p>

那么在代码中就是:

matchesRef
    .queryOrdered(byChild: "users/user-1")
    .queryStarting(atValue: "ok", childKey: "<last-fetched-user-id>")
    .queryLimited(toFirst: 5)
    .observe(.value, with: { snapshot in