简体   繁体   English

Firestore 分页:StartAfter 查询与 DocumentSnapshot 不工作

[英]Firestore Pagination : StartAfter query with DocumentSnapshot not working

I have a list of offers/deals saved in my firestore database.我的 firestore 数据库中保存了一份报价/交易列表。 I want to fetch the first 3 deals sorted by discount in desc order ie the deals with highest discount comes first and then next highest and so on.我想获取按降序折扣排序的前 3 笔交易,即折扣最高的交易排在第一位,然后是次高的,依此类推。 I am fetching them in segments of 3.我以 3 段的形式获取它们。

我如何对它们进行分类 firebase 控制台

我根据折扣排序的数据库

I am trying to achieve the same using android code.我正在尝试使用 android 代码实现相同的目的。 So, that the first 3 elements with discount 55,44,28 should come first, later 27,27,21 and finally 19,4.因此,折扣为 55、44、28 的前 3 个元素应该首先出现,然后是 27、27、21,最后是 19、4。

class ContentGenerationActivityV1 : AppCompatActivity() {

     var lastDocument : DocumentSnapshot?= null
     lateinit var filterQuery: Query

     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_content_generation)

        val doc = FirebaseFirestore.getInstance().collection("steal-deals").document("deals")

       filterQuery = doc.collection(getTodayTimeStamp().toString())
        .orderBy("discount", Query.Direction.DESCENDING)

       filterFetchButton.setOnClickListener {
           if(lastDocument == null){
              fetchFirstFew()
           } else {
            fetchMore()
        }
    }
}

//Get first 3 elements
fun fetchFirstFew(){

    Log.d("N/W CALL ::", "FIRST")

    filterQuery.limit(3) .get()
        .addOnSuccessListener { result ->

            lastDocument = result.documents.last()

            for (document in result) {
                Log.d("DEAL :: ", "${document.id} => ${document.data}")
            }

        }


}

//Get the next 3 elements
fun fetchMore(){

    Log.d("N/W CALL ::", "SECOND")

    filterQuery.startAfter(lastDocument)
        .limit(3).get()
        .addOnSuccessListener { result ->

            lastDocument = result.documents.last()

            for (document in result) {
                Log.d("DEAL :: ", "${document.id} => ${document.data}")
            }


        }
}

But it is always returning the first three elements (55,44,28) no matter what.但无论如何它总是返回前三个元素 (55,44,28)。 在此处输入图像描述

Please guide me so in achieving the same.请指导我实现同样的目标。

I had the same problem but after some research found out the "startAfter(lastDocument)" returns a new Query instance which you need to get and on that you need to call limit.我遇到了同样的问题,但经过一些研究发现“startAfter(lastDocument)”返回一个新的查询实例,您需要获取该实例,并且您需要调用limit。

val query = filterQuery.startAfter(lastDocument)
query.limit(3).get().addOnSuccessListener {}

i just got this issue.我刚收到这个问题。

reference参考

so, in your code所以,在你的代码中

Wrong:错误的:

filterQuery.limit(3) .get()
        .addOnSuccessListener { result ->

            lastDocument = result.documents.last()

            for (document in result) {
                Log.d("DEAL :: ", "${document.id} => ${document.data}")
            }

        }

True:真的:

filterQuery = filterQuery.limit(3) .get()
        .addOnSuccessListener { result ->

            lastDocument = result.documents.last()

            for (document in result) {
                Log.d("DEAL :: ", "${document.id} => ${document.data}")
            }

        }

because when you chaining to query, it creates and returns a new Query that starts after the provided document (exclusive).因为当您链接到查询时,它会创建并返回一个在提供的文档(独占)之后开始的新查询。 so you have to reassign it!所以你必须重新分配它!

In addition to storing query object in separate reference, you also need to make sure that you are passing the non-null lastDocument to startAfter.除了在单独的引用中存储查询 object 之外,您还需要确保将非空lastDocument 传递给 startAfter。 You can either cast it to as Document Snapshot or put !!您可以将其转换为文档快照或将 !! at the variable end在可变端

filterQuery.startAfter(lastDocument!!)
    .limit(3).get()

OR或者

filterQuery.startAfter(lastDocument as DocumentSnapshot)
    .limit(3).get()

It will surely work它一定会起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM