Ad

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.

Ad

Answer

I finally find the problem,

in .startAfter(lastDocument)

that lastDocument still in nullable type (DocumentSnapshot?), it shouldn't.

Ad
source: stackoverflow.com
Ad