Reputation: 571
I am trying to merge 2 queries to pass them as Options to display on my FirestoreRecyclerView. All the methods I found would merge only the Tasks and therefore I can pass the task on setQuery. Below is my query1 and query2 and I need to merge them in one query to display. How can I achieve this?
val query1 = firestoreRepo.getAllGames()
.whereEqualTo(Constants.PLAYER1ID, currentUserID)
.whereEqualTo("player2Id", argsId)
val query2 = firestoreRepo.getAllGames().whereEqualTo("player2Id", currentUserID)
.whereEqualTo("player1Id", argsId)
val options: FirestoreRecyclerOptions<Games> = FirestoreRecyclerOptions.Builder<Games>()
.setQuery(query, Games::class.java)
.setLifecycleOwner(this)
.build()
Upvotes: 1
Views: 741
Reputation: 138944
Unfortunately, you cannot pass two queries to FirestorePagingOptions, only one is allowed. If you need the result of two queries, then you should use Tasks.whenAllSuccess() method, like in the following lines of code:
val query1 = firestoreRepo.getAllGames()
.whereEqualTo(Constants.PLAYER1ID, currentUserID)
.whereEqualTo("player2Id", argsId)
val query2 = firestoreRepo.getAllGames()
.whereEqualTo("player2Id", currentUserID)
.whereEqualTo("player1Id", argsId)
val firstTask = query1.get()
val secondTask = query2.get()
val combinedTask: Task<*> = Tasks.whenAllSuccess<Any>(firstTask, secondTask).addOnSuccessListener {
//Do what you need to do with your list
}
When overriding the onSuccess() method, the result is a list of objects. Because both queries return objects of type Games
, you can simply cast the objects from the list to objects of type Games
. In that case, you won't be able to use the Firebase-UI library anymore. Not perfect, but it will do the trick.
Upvotes: 1