【问题标题】:Iterating Firestore Android collection on basis of ID or attribute根据 ID 或属性迭代 Firestore Android 集合
【发布时间】:2019-02-03 18:42:17
【问题描述】:

我想迭代一个从集合文档 ID 开始的集合。

有没有像下面这样的方法,可以让我们从 id 或属性 id 开始读取数据?

 db.collection("questions").startReadingDocumentAt("02cubnmO1wqyz6yKg571 ").limit(10).get() 

或

db.collection("questions").startReadingWhereEqualTo("questionID","02cubnmO1wqyz6yKg571 ").limit(10).get()

我知道我可以根据 Id 读取 DocumentSnapShot,然后可以开始使用 lastVisible 项进行迭代。但这花了我两次读取操作。可以一次性完成吗?

Firestore-root
    |
    --- questions (collections)
    |     |
    |     --- 02cubnmO1wqyz6yKg571 (questionId document) // myID and my questionID is same
    |            |
    |            --- questionId: "02cubnmO1wqyz6yKg571"
    |            |
    |            --- title: "Question Title"
    |            |
    |            --- date: August 27, 2018 at 6:16:58 PM UTC+3
    |            |

我已经去了through this

说明

我不想做RecyclerView pagination。我想在每次活动开始时加载下 10 个问题。所以我以同样的方式调用查询,但它需要我保存 DocumentSnapShot。所以我决定保存 questionID 并在此基础上读取数据...

【问题讨论】:

    标签: java android firebase pagination google-cloud-firestore


    【解决方案1】:

    没有:

    startReadingDocumentAt("02cubnmO1wqyz6yKg571")
    

    也没有

    startReadingWhereEqualTo("questionID","02cubnmO1wqyz6yKg571")
    

    在 firestore 但根据您的数据库架构,要解决此问题,您可以使用以下查询:

    db.collection("questions").
        whereGreaterThanOrEqualTo("questionID","02cubnmO1wqyz6yKg571")
        .limit(10)
        .get()
        addOnCompleteListener.(/* ... */)
    

    更多信息here:

    创建并返回一个带有附加过滤器的新查询,该过滤器文档必须包含指定的字段,并且值应该大于或等于指定的值。

    【讨论】:

    【解决方案2】:

    您可以使用query cursors 执行此操作。例如:

    db.collection("questions")
        .orderBy("questionID")
        .startAt("02cubnmO1wqyz6yKg571")
        .limit(10)
        .get()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多