I use a FirebaseDatabase that holds "categories" and related "details" like this:
Note: categoryId in "categories" acts as primary key for "details".
My DetailAdapter works with FirebaseUI's RecyclerAdapter using this query:
class DetailAdapter(lifecycleOwner: LifecycleOwner, private val categoryId: String) : FirebaseRecyclerAdapter<Detail, DetailAdapter.DetailHolder>(buildOptions(lifecycleOwner, categoryId)) {
companion object {
private fun buildQuery(categoryId: String) = FirebaseDatabase.getInstance()
.reference
.child("").child("details").child(categoryId)
.limitToLast(50)
private fun buildOptions(lifecycleOwner: LifecycleOwner, categoryId: String) = FirebaseRecyclerOptions.Builder<Detail>()
.setQuery(buildQuery(categoryId), Detail::class.java)
.setLifecycleOwner(lifecycleOwner)
.build()
}
...
This works flawlessly.
Now I want to write a similar Adapter (i.e. named "FavoritesAdapter") in order to display only "details" that have "favorite: [set to] true". Can I use buildQuery to achieve this?
There is no way to use buildQuery to achieve this. The key to a solution is a strategy called Denormalization, which is a polite way of saying "Duplicating Data". You may want to watch this for an explanation: Denormalization is normal with Firebase Database.