【问题标题】:Android - Firestore returns an empty query using startAfter() and limit()Android - Firestore 使用 startAfter() 和 limit() 返回一个空查询
【发布时间】:2019-06-24 05:55:21
【问题描述】:

我正在尝试使用 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 得到项目:0pqcRzSd06WWlNNmcolu

TrendingRepo:在位置:1 得到项目:7I7wiSYt5yEBWwN08bqJ

...

TrendingRepo:位置:45 得到项目:4B3dEPhFLqhKrYpLWYE7

TrendingRepo:在位置:46 得到项目:4ddLqiGe8ReXW8SKq2Q6

TrendingRepo:在位置:47 得到项目:4uVnnGNAmKvGUUHcV01n

TrendingRepo: pagination size : 48 // 第二次尝试

//不再记录,数据为空

【问题讨论】:

  • 签出this。是 Java 代码,但希望对您有所帮助。

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


【解决方案1】:

可能存在item小于分页大小的情况所以这里是代码

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()
            }

希望对你有帮助。

【讨论】:

  • 项目加载成功,我用 => if (lastVisible != null) { newQuery = newQuery.startAfter(lastVisible!!) } 然后设置限制获取页面。我正要打破我的 MVP 架构并切换到 FirestorePagingAdapter,谢谢!我已经在我的自定义适配器中使用 isLoading 和 allData 布尔值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
  • 2019-10-28
  • 1970-01-01
  • 2019-05-01
  • 2018-06-09
相关资源
最近更新 更多