【发布时间】:2020-07-22 12:45:06
【问题描述】:
我已经根据文档实现了分页。我在我的项目中使用此代码:
override fun onEvent(querySnapshot: QuerySnapshot?, e: FirebaseFirestoreException?) {
if (e != null) return
for (documentChange in querySnapshot!!.documentChanges) {
value = when (documentChange.type) {
Type.ADDED -> {
Log.d("TAG", "oldIndex:" + documentChange.oldIndex + "/" + "newIndex:" + documentChange.newIndex)
}
Type.MODIFIED -> {
Log.d("TAG", "oldIndex:" + documentChange.oldIndex + "/" + "newIndex:" + documentChange.newIndex)
}
Type.REMOVED -> {
Log.d("TAG", "oldIndex:" + documentChange.oldIndex + "/" + "newIndex:" + documentChange.newIndex)
}
}
}
}
当我使用此查询获取前 4 个用户时:
var query: Query = db.collection("users")
.orderBy("name", DESCENDING)
.limit(4)
我明白了:
A/oldIndex:-1/newIndex:0
B/oldIndex:-1/newIndex:1
C/oldIndex:-1/newIndex:2
D/oldIndex:-1/newIndex:3
这是正确的,因为所有用户都是新用户并且 oldIndex 等于 -1。 newIndex 从 0 增加到 3,这也是正确的。但是,当我使用此查询时:
var secondQuery: Query = db.collection("users")
.orderBy("name", DESCENDING)
.startAfter(lastVisibleUser)
.limit(4)
为了获得接下来的 4 个用户,我得到:
E/oldIndex:-1/newIndex:0
F/oldIndex:-1/newIndex:1
G/oldIndex:-1/newIndex:2
H/oldIndex:-1/newIndex:3
在用户名正确的地方,oldIndex 又是正确的,但 newIndex 从 0 开始,不是应该是从 4 开始。如何才能第二次获得正确的 newIndex?
【问题讨论】:
标签: android firebase kotlin google-cloud-firestore