如何在Firebase数据库中进行分页#AskFirebase [英] How to paginate in Firebase Database #AskFirebase

查看:41
本文介绍了如何在Firebase数据库中进行分页#AskFirebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一种类似于个人词汇的应用程序.该数据库具有以下形式.

现在,我需要实现分页,部分检索用户的单词,但保留词典顺序.保持单词作为键(/user/{uid}/words/{word} )是不合适的,因为将来将无法处理同形图(因为它们的键将重合).我决定为每个用户保留其他属性 word ,以便可以调用 db.getReference().child("users").child(uid).child("words").orderByChild("word").

这将检索用户的所有单词.现在我需要对这个查询进行分页,例如首先下载20个单词,然后再下载20个,依此类推,但要保留字典顺序.

  {用户":{"yXYSqB016JMr1FIc85pvMbvqDDt2":{字" : {"5v1a1PaDKnTvvOH19kaFTa1iyOx2":{索引":1"word":"apple"},"kXHakBKxk9TrAlWL1vTOCe0akk80":{索引":2"word":"house"},"xSKSqB312JMrsFig15pvMbvqAAt0":{...}}},"zCAtMpl9uxSjG9dJarGktTTs20w2":{...}},词汇" : {"en":{"5v1a1PaDKnTvvOH19kaFTa1iyOx2":{定义":{在树上生长的水果":true},"word":"apple"},"kXHakBKxk9TrAlWL1vTOCe0akk80":{...},"xSKSqB312JMrsFig15pvMbvqAAt0":{...}}}} 

解决方案

您似乎来自SQL思维方式,在其中通过指定要获取的项目数和要跳过的项目数进行分页.这是基于索引的分页.

Firebase的另一端使用基于光标的分页.您告诉它要获取的项目数量,并在开始(或结束)项目.您可以通过订购属性的值(在这种情况下为 word 的值)来标识此项.由于相同的值可能会出现在多个子项中,因此您还可以指定要在其开始/结束的子项的键(以 5v1a1 ... 开头的键)作为第二个参数.

因此,假设您的页面大小为2.您会获得前两个字:

  DatabaseReference allWords = db.getReference().child("users").child(uid).child("words");查询firstPage = allWords.orderByChild("word").limitToFirst(2); 

当您为此添加一个侦听器时,您将获得前两个单词.您需要记住第一页中的 word 和最后一个单词的键:

  String lastWordOnPreviousPage ="house";字符串lastKeyOnPreviousPage ="5v1a1 ..."; 

现在,如果您需要两个单词的第二页,则可以通过以下方式获得它们:

  Query secondPage = allWords.orderByChild("word").startAt(lastWordOnPreviousPage,lastKeyOnPreviousPage).limitToFirst(2); 

I am creating an application that is some kind of a personal vocabulary. The database is of the following form.

Now I need to implement a pagination, partial retrieval of the words of a user, but preserving the lexicographical order. Keeping words as keys (/user/{uid}/words/{word}) is not suitable, because handling homographs will be impossible in the future (as their key will coincide). I decided to keep additional property word for each user, so that I can call db.getReference().child("users").child(uid).child("words").orderByChild("word").

This will retrieve all words of a user. Now I need to paginate this query, e.g. first download 20 words and then again 20 etc., but preserving lexicographical order.

{
  "users" : {
    "yXYSqB016JMr1FIc85pvMbvqDDt2" : {
      "words" : {
        "5v1a1PaDKnTvvOH19kaFTa1iyOx2" : {
          "index" : 1,
          "word" : "apple"
        },
        "kXHakBKxk9TrAlWL1vTOCe0akk80" : {
          "index" : 2,
          "word" : "house"
        },
        "xSKSqB312JMrsFig15pvMbvqAAt0" : { ... }
      }
    },
    "zCAtMpl9uxSjG9dJarGktTTs20w2" : { ... }
  },
  "vocabulary" : {
    "en" : {
      "5v1a1PaDKnTvvOH19kaFTa1iyOx2" : {
        "definitions" : {
          "a fruit that grows on a tree" : true
        },
        "word" : "apple"
      },
      "kXHakBKxk9TrAlWL1vTOCe0akk80" : { ... },
      "xSKSqB312JMrsFig15pvMbvqAAt0" : { ... }
    }
  }
}

解决方案

You seem to come from a SQL way of thinking, where you paginate by specifying the number of items to get and the number of items to skip. This is index-based pagination.

Firebase on the other uses cursor-based pagination. You tell it the nimber of items to get and at which item to start (or end). You identify this item by the value of the property on which you order, in your case that is the value of word. Since the same value could potentially appear in multiple children, you can also specify the key (the thing starting with 5v1a1...) of the child at which to start/end as a second parameter.

So say that you have a page size of two. You get the first 2 words with:

DatabaseReference allWords = db.getReference().child("users").child(uid).child("words");
Query firstPage = allWords.orderByChild("word").limitToFirst(2);

When you attach a listener to this, you'll get the first two words. You'll need to remember the word and the key of the last word in this first page:

String lastWordOnPreviousPage = "house";
String lastKeyOnPreviousPage = "5v1a1...";

Now if you need the second page of two words, you get them by:

Query secondPage = allWords.orderByChild("word").startAt(lastWordOnPreviousPage, lastKeyOnPreviousPage).limitToFirst(2);

这篇关于如何在Firebase数据库中进行分页#AskFirebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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