1

i need to display a list of entities accompanied with a "remaining" count that is calculated by two queries ("getting target count" minus "getting actual count").

Like:

EntryA - 4
EntryB - 2
EntryC - 0
EntryD - 1

I'd like to serve the combined data to the RecyclerView-Adapter in order to avoid queries in there and I need to listen to changes on the actual count.

In the end I'd need sth like

entriesWithCount = MutableLiveData<List<EntryWithCount>>()

But now I'm struggling to use transformations on the queries.

So I'd need to get the actual count and do a switchMap, but for getting the acutal count I'd already need the entryId.

entries = dao.getEntries() // LiveData
entries.map { entry ->
   val targetCount = dao.getTargetCount(entry.Id)

   Transformations.switchMap(dao.getActualCount(entry.Id)) {actualCount ->
       // Create EntryWithCount(entry, targetCount - actualCount)
   }
}
cwiesner
  • 934
  • 1
  • 8
  • 24

0 Answers0