Answer the question
In order to leave comments, you need to log in
How to return modified list from db using rxjava?
There is a method that returns data as Flowable<MutableList<Item>>
. The method itemDao.getAllItems()
returns a list in the form Flowable<MutableList<ItemEntity>>
from the database. To convert an object from ItemEntity to Item , I use my Mapper class using the method mapDetailItem
, where I throw a list of parts from another table, which will then be added to the Item object , and then the object itself will be returned, which already has a filled field List<DetailItem>
.
How can I do this with each element and end up returning a list MutableList<Item>
instead ofMutableList<ItemEntity>
override fun loadLocalItems(): Flowable<MutableList<Item>> {
return itemDao.getAllItems()
.map { list ->
Flowable.fromIterable(list).map {
mapper.mapDetailItem(it, itemDao.getAllDetailItemsById(it.idItem))
}
}
}
return itemDao.getItemById(id).map { mapper.mapDetailItem(it, itemDao.getAllDetailItemsById(it.idItem)) }
Answer the question
In order to leave comments, you need to log in
1) All rx interfaces must return a List, not a MutableList! It's like a postulate. The data inside rx must be immutable!
2) instead of map {Floawable.fromIterable(it)} you can use flatMapIterable
3) getAllDetailItemsById must also return a stream. Why is there going to the database in the map? work with it like this
.flatMapIterable {
itemDao.allDetailItemsById(it.idItem)
.map { data -> mapper.mapDetailItem(it, data) }
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question