亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Firestore 使用 startAfter() 和 limit() 返回空查詢

Firestore 使用 startAfter() 和 limit() 返回空查詢

HUWWW 2023-02-16 16:45:56
我正在嘗試使用 Firebase Firestore startAfter()和limit()查詢方法實現分頁系統。第一個查詢成功返回,但第二個查詢返回空快照。這是我的getNextPage()方法:fun getNextPage(paginationSize : Long) : TrendingRepository {database.collection("app")    .document("data")    .collection("offers")    .orderBy("discount")    .startAfter(lastVisible)    .limit(paginationSize)    .get().addOnSuccessListener { snapshot ->        Log.i("TrendingRepo", "pagination size : $paginationSize")        val newList = ArrayList<Offer>()        if (!snapshot.isEmpty) {            lastVisible = snapshot.documents[snapshot.size() - 1]        }        for (document in snapshot) {            val item = document.toObject(Offer::class.java)            newList.add(item)            Log.i("TrendingRepo", "at position: ${newList.indexOf(item)} got item: ${item.id}")        }        successListener?.onSuccess(newList)    }.addOnFailureListener {        failureListener?.onFailure(it.localizedMessage)    }return this}這是我的 Logcat:TrendingRepo: pagination size : 48 // 第一次嘗試TrendingRepo:在位置:0 得到項目:0pqcRzSd06WWlNNmcoluTrendingRepo:在位置:1 得到了項目:7I7wiSYt5yEBWwN08bqJ...TrendingRepo:在位置:45 得到項目:4B3dEPhFLqhKrYpLWYE7TrendingRepo:在位置:46 得到項目:4ddLqiGe8ReXW8SKq2Q6TrendingRepo:在位置:47 得到項目:4uVnnGNAmKvGUUHcV01nTrendingRepo: pagination size : 48 // 第二次嘗試//不再記錄,數據為空
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

可能存在項目小于分頁大小的情況所以這里是代碼


private var lastVisible: DocumentSnapshot? = null

private var isLastPage: Boolean = false

private var isDocEmpty: Boolean = false


var ref: Task<QuerySnapshot>? = null


 if (lastVisible != null) {

ref = database.collection("app").document("data").collection("offers").orderBy("discount").startAfter(lastVisible).limit(paginationSize).get()

 } else {

ref = database.collection("app").document("data").collection("offers").orderBy("discount").limit(paginationSize).get()

 }



 ref.addOnSuccessListener { documents ->


            hideProgress()

            isDocEmpty = documents.isEmpty




            if (!isDocEmpty) {

                lastVisible = documents.last()

                isLastPage = documents.size() < paginationSize

            }


            isLoading = false

        }

            .addOnFailureListener { exception ->

                Log.w("TAG", "Error getting documents: ", exception)

                isLoading = false

                hideProgress()

            }

我希望這會幫助你。


查看完整回答
反對 回復 2023-02-16
  • 1 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號