I have a list of offers/deals saved in my firestore database. I want to fetch the first 3 deals sorted by discount in desc order ie the deals with highest discount comes first and then next highest and so on. I am fetching them in segments of 3.
I am trying to achieve the same using android code. So, that the first 3 elements with discount 55,44,28 should come first, later 27,27,21 and finally 19,4.
class ContentGenerationActivityV1 : AppCompatActivity() {
var lastDocument : DocumentSnapshot?= null
lateinit var filterQuery: Query
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_content_generation)
val doc = FirebaseFirestore.getInstance().collection("steal-deals").document("deals")
filterQuery = doc.collection(getTodayTimeStamp().toString())
.orderBy("discount", Query.Direction.DESCENDING)
filterFetchButton.setOnClickListener {
if(lastDocument == null){
fetchFirstFew()
} else {
fetchMore()
}
}
}
//Get first 3 elements
fun fetchFirstFew(){
Log.d("N/W CALL ::", "FIRST")
filterQuery.limit(3) .get()
.addOnSuccessListener { result ->
lastDocument = result.documents.last()
for (document in result) {
Log.d("DEAL :: ", "${document.id} => ${document.data}")
}
}
}
//Get the next 3 elements
fun fetchMore(){
Log.d("N/W CALL ::", "SECOND")
filterQuery.startAfter(lastDocument)
.limit(3).get()
.addOnSuccessListener { result ->
lastDocument = result.documents.last()
for (document in result) {
Log.d("DEAL :: ", "${document.id} => ${document.data}")
}
}
}
But it is always returning the first three elements (55,44,28) no matter what.
Please guide me so in achieving the same.
I had the same problem but after some research found out the "startAfter(lastDocument)" returns a new Query instance which you need to get and on that you need to call limit.
val query = filterQuery.startAfter(lastDocument)
query.limit(3).get().addOnSuccessListener {}
i just got this issue.
so, in your code
Wrong:
filterQuery.limit(3) .get()
.addOnSuccessListener { result ->
lastDocument = result.documents.last()
for (document in result) {
Log.d("DEAL :: ", "${document.id} => ${document.data}")
}
}
True:
filterQuery = filterQuery.limit(3) .get()
.addOnSuccessListener { result ->
lastDocument = result.documents.last()
for (document in result) {
Log.d("DEAL :: ", "${document.id} => ${document.data}")
}
}
because when you chaining to query, it creates and returns a new Query that starts after the provided document (exclusive). so you have to reassign it!
In addition to storing query object in separate reference, you also need to make sure that you are passing the non-null lastDocument to startAfter. You can either cast it to as Document Snapshot or put !! at the variable end
filterQuery.startAfter(lastDocument!!)
.limit(3).get()
OR
filterQuery.startAfter(lastDocument as DocumentSnapshot)
.limit(3).get()
It will surely work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.