Firebase查询与限制/偏移可能的解决方案 [英] Firebase query with limit / offset possible solutions

查看:109
本文介绍了Firebase查询与限制/偏移可能的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从限制和抵消的火力点检索数据?例如,我在我的firebaseRef中有大约1000个元素,并且想要开发某种分页。有没有一种方法,我可以做到这一点,而无需加载完整的对象列表。



现在我使用 queryLimitedToLast(limit)来做,并为每一个下一页增加 limit 。但这种方法不允许我得到页数。



更新:

有一件事我没有提到,但是非常重要。我的数据显示从上次到第一次。想象一下,这是一个简单的信使,但分页。所以我想显示最后20个项目,然后20个这样的20个等。

谢谢。

解决方案

Yup通过一个长长的子节点列表进行分页是绝对有可能的。诀窍是记住当前页面最后一项的关键字(或优先级或任何值),然后将它传递给 queryStartingAt ...



加藤在他的博客文章中写了一个很好的例子如何在Firebase中实现常见的SQL查询

  //获取小部件的第2页
new Firebase(https://examples-sql-queries.firebaseio.com/widget)
.startAt(null,lastWidgetOnPrevPage)
.limitToFirst(LIMIT + 1)//添加一个限制来解释lastWidgetOnPrevPage
.once('value',function(snap){
var vals = snap.val()|| {} ;
删除vals [lastWidgetOnPrevPage]; //删除多余的记录
console.log('widgets on this page',vals);
});

这个例子是使用JavaScript SDK的,但是相关的方法也可以在iOS SDK。



请注意,此片段检索一个额外项目( LIMIT + 1 ),因为我们从上一页的最后一项开始。


Is there a way to retrieve data from firebase with limit and offset? For example I have about 1000 elements in my firebaseRef and want to develop some kind of pagination. Is there a way I can do it without loading full list of objects.

For now I'm using queryLimitedToLast(limit) to do it and increase limit for every next page. But this method do not allow me to get pages count.

UPDATE:

One thing I did not mention but it's very important. My data showing from-last-to-first. Imagine it's a simple messenger but with pagination. So I want to show last 20 items, then 20 before these 20 etc.

Thanks.

解决方案

Yup, paging through a long list of child nodes is definitely possible. The trick is to remember the key (or priority or whatever value you sort on) of the last item on the current page and then pass that into queryStartingAt... for the next page.

Kato wrote up a great example in his blog post on how to implement common SQL queries in Firebase:

// fetch page 2 of widgets
new Firebase("https://examples-sql-queries.firebaseio.com/widget")
   .startAt(null, lastWidgetOnPrevPage)
   .limitToFirst(LIMIT+1) // add one to limit to account for lastWidgetOnPrevPage
   .once('value', function(snap) {
      var vals = snap.val()||{};
      delete vals[lastWidgetOnPrevPage]; // delete the extraneous record
      console.log('widgets on this page', vals);
   });

This example is using the JavaScript SDK, but the relevant methods are also available in the iOS SDK.

Note that this snippet retrieves one extra item (LIMIT+1), since we start on the last item of the previous page.

这篇关于Firebase查询与限制/偏移可能的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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