与firebase过滤分页列表 [英] Filtered paginated list with firebase

查看:140
本文介绍了与firebase过滤分页列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用firebase和swift做一个分页的过滤列表(但是随意用最喜欢的编程语言回答),而不用过滤客户端上的检索数据。

让我们假设我有这个结构

$ p $匹配
匹配-1
名称:匹配1
用户
用户1:确定
用户2:真实
匹配-2
名称:匹配2
用户
user-1:ok
user-2:true
user-3:true
match-3
名称:Match 3
用户
user-1:true
user-2:true
user-3:true
...

现在我想得到一个用户1的匹配值为ok的分页列表。



我正在做这样的事情

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

});

但是会导致崩溃,因为之前调用queryStartingAtValue或queryEqualToValue之后无法调用queryStartingAtValue

有什么建议吗?

解决方案

传入第一个项的,作为第二个参数返回到 queryStarting 。从文档(重点mine):

$ block
$ p $ query code $ queryStartingAtValue:childKey:用于生成对有限的数据在这个位置的看法。由 queryStartingAtValue:childKey 返回的 FIRDatabaseQuery 实例将响应值大于startValue的节点上的事件,或等于startValue,并且一个键大于或等于childKey


所以在代码中:

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


I'm trying to make a paginated filtered list with firebase and swift (but feel free to answer with the programming language that you like the most) without filter the retrieved data on the client.

Let's suppose I've this structure

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

Now I want to get a paginated list of all the matches of the user-1 that have the value "ok"

I'm doing something like this

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

});

But it causes a crash because "Can't call queryStartingAtValue: after queryStartingAtValue or queryEqualToValue was previously called"

Any suggestions?

解决方案

If I recall correctly, you can pass in the key of the first item to return as a second argument to queryStarting. From the documentation (emphasis mine):

queryStartingAtValue:childKey: is used to generate a reference to a limited view of the data at this location. The FIRDatabaseQueryinstance returned by queryStartingAtValue:childKey will respond to events at nodes with a value greater than startValue, or equal to startValue and with a key greater than or equal to childKey.

So in code that would then be:

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

这篇关于与firebase过滤分页列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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