Firestore分页不适用于按浮点值排序的查询

时间:2019-06-23 19:23:28

标签: android firebase pagination google-cloud-firestore

我被Firestore分页卡住了。我试图使查询按浮动值(等级)排序。 如果我对按名称排序的查询使用分页,它将很好地工作。但是通过评级,它可以循环加载第一页,并且无法通过最后看到的uid获取新用户页面。 原因听起来可能是这样的:部分用户具有最高的唯一评分,但大多数用户具有相同的评分,例如10位用户将获得14.4分? 如果您能给我一些建议或一些技巧来解决这个奇怪的错误,我将非常感激。

这是分页代码:

class SearchRepository {
companion object {
    const val ITEMS_PER_PAGE = 20
}

private var lastUserInPage = ""
private val reference: CollectionReference = FirebaseFirestore.getInstance().collection(Consts.USERS_DB)

//todo: rename getUsersNextPage(isFirstPage: Boolean)
fun getUsers(filterParams: List<SearchModel>, usersListCallback: UsersListCallback, page: Int) {
    var query: Query = reference

    if (!filterParams.isEmpty()) {
        for ((key, value) in filterParams) {
            query = query.whereEqualTo(key, value)
        }
    }

    //not forget to set to zero when using from another fragment
    if (page == 0)
        lastUserInPage = ""

    query
            .orderBy(Consts.RATING)
            .startAt(lastUserInPage)
            .limit(ITEMS_PER_PAGE.toLong())
            .get()
            .addOnCompleteListener { task ->
            putCallbackData(usersListCallback, task) }
}


private fun putCallbackData(usersListCallback: UsersListCallback, task: Task<QuerySnapshot>) {
    val fsUserList = ArrayList<FsUser>()
    for (snapshot in task.result!!.documents) {
        if (isUserExist()) {
            if (snapshot.id != getUid()) {
                fsUserList.add(snapshot.toObject(FsUser::class.java)!!)
            }
        } else {
            fsUserList.add(snapshot.toObject(FsUser::class.java)!!)
        }
    }

    lastUserInPage = fsUserList[fsUserList.size - 1].uid

    // some firebase stuff
    if (fsUserList.size == ITEMS_PER_PAGE) {
        fsUserList.removeAt(fsUserList.size - 1)
    } else if (fsUserList.size == ITEMS_PER_PAGE - 1 && isUserExist()) {
        fsUserList.removeAt(fsUserList.size - 1)
    }


    usersListCallback.setUsers(fsUserList)
}

}

0 个答案:

没有答案