L
L
Lordao2019-03-21 16:03:19
Android
Lordao, 2019-03-21 16:03:19

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))
                    }
                }
 }

With one element I do this way
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 answer(s)
D
Denis Zagaevsky, 2019-03-21
@Lordao

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) }
}

4) " which will then be added to the Item object, and then the object itself will be returned, which already has a completed List field " - see point 1). Item must be immutable! If you need to add something to it, then data class + copy(something = 123)
5) you can convert the stream into a list with a simple toList() method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question