Why I Always Get Empty Documents When Using Pagination In Firestore
I am trying to use pagination by using document snapshot to define the query cursor.
when to fragment is opened for the first time, in onCreateView I use the code below to get 7 events from firestore
fun getAllSearchedEventsFromTheBeginning(startDate: Date, endDate: Date, selectedCity: String, selectedEventType: String, limit: Long, completion: (errorMessage:String?, events: ArrayList<Event>?, lastDocument: DocumentSnapshot?) -> Unit) {
// not only free events, paid events are also included
FirestoreCollectionReference.event.getReference()
.whereEqualTo(FIRESTORE_EVENT_FIELD_CITY,selectedCity)
.whereEqualTo(FIRESTORE_EVENT_FIELD_EVENT_TYPE, selectedEventType)
.whereEqualTo(FIRESTORE_EVENT_FIELD_HAS_BEEN_APPROVED,true)
.whereGreaterThan(FIRESTORE_EVENT_FIELD_DATE_START_TIME,startDate)
.whereLessThan(FIRESTORE_EVENT_FIELD_DATE_START_TIME,endDate)
.orderBy(FIRESTORE_EVENT_FIELD_DATE_START_TIME, Query.Direction.ASCENDING)
.limit(limit)
.get()
.addOnSuccessListener { snapshot ->
val lastDocument = snapshot.documents[snapshot.size() - 1]
val eventDocuments = snapshot.documents
val eventArray = ArrayList<Event>()
for (document in eventDocuments) {
val eventData = document.data
val event = Event(dataEvent = eventData)
eventArray.add(event)
}
completion(null,eventArray, lastDocument)
}.addOnFailureListener {
completion(it.localizedMessage,null,null)
}
}
I am using lamda expression, to send the lastVisible document, and that lastVisible document will be used as the starting point for my next query
after reaching the bottom of my recycler view, then I use the code below to get the next 7 documents from firestore
fun getAllSearchedEventsAfterLastDocument(startDate: Date, endDate: Date, selectedCity: String, selectedEventType: String, limit: Long, lastDocument: DocumentSnapshot?, completion: (errorMessage:String?, events: ArrayList<Event>?, lastDocument: DocumentSnapshot?) -> Unit) {
// not only free events, paid events are also included
FirestoreCollectionReference.event.getReference()
.whereEqualTo(FIRESTORE_EVENT_FIELD_CITY,selectedCity)
.whereEqualTo(FIRESTORE_EVENT_FIELD_EVENT_TYPE, selectedEventType)
.whereEqualTo(FIRESTORE_EVENT_FIELD_HAS_BEEN_APPROVED,true)
.whereGreaterThan(FIRESTORE_EVENT_FIELD_DATE_START_TIME,startDate)
.whereLessThan(FIRESTORE_EVENT_FIELD_DATE_START_TIME,endDate)
.orderBy(FIRESTORE_EVENT_FIELD_DATE_START_TIME, Query.Direction.ASCENDING)
.limit(limit)
.startAfter(lastDocument)
.get()
.addOnSuccessListener { snapshot ->
val eventDocuments = snapshot.documents
if (eventDocuments.isEmpty()) {
completion("Event is empty",null, null)
} else {
val lastDocument = snapshot.documents.last()
val eventArray = ArrayList<Event>()
for (document in eventDocuments) {
val eventData = document.data
val event = Event(dataEvent = eventData)
eventArray.add(event)
}
completion(null,eventArray, lastDocument)
}
}.addOnFailureListener {
completion(it.localizedMessage,null,null)
}
}
I am sure that I send the same parameters to both of those function, and the last documents is also correct, it exactly the same as the last document that appears in my recycler view.
but I always get empty documents if called that second function getAllSearchedEventsAfterLastDocument
and this line below always triggered in getAllSearchedEventsAfterLastDocument
.
if (eventDocuments.isEmpty()) {
completion("Event is empty",null, null)
}
please help me, I am confused.
Answer
I finally find the problem,
in .startAfter(lastDocument)
that lastDocument
still in nullable type (DocumentSnapshot?), it shouldn't.
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View