Reputation: 18118
Hi i am trying to use rxjava along with a LiveData that gets transformed using Map before i return it. However it saying i need to return something from inside the map.
I tried doing return Single.just(pianoStorage.getAllFavItems())
and it complains that you are not allowed to return something?
override fun getFavouriteItems() : Single<LiveData<MutableList<Items>>>{
//check if db has offers
if (storage.getAllFavItems().value!!.isEmpty()){
getAllFavItems().map {
{offers : ItemsResponse ->
storage.saveAllItemsLists(offers)
Single.just(storage.getAllFavItems())
}
}.subscribe(
{offers ->
return@subscribe
}
)
}else{
return Single.just(storage.getAllFavItems())
}
}
The code below basically checks my persistent database if i have some items there and if not, fetch it from the network using retrofit(the getAllFavItems())
This network call gets all the list of items and then what i need to do is first save it to the storage and then get only the favourite items from it afterwords using the below:
@Query("SELECT * FROM items WHERE state = 'Fav'")
fun getAllFavItems(): LiveData<MutableList<Item>>
Upvotes: 1
Views: 814
Reputation: 13321
If you change your DAO
definition to:
@Query("SELECT * FROM items WHERE state = 'Fav'")
fun getAllFavItems(): Flowable<MutableList<Item>>
You can do something like this:
getAllFavItems()
.flatMap({
if(it.isEmpty()) {
getAllFavItems().toFlowable(BackpressureStrategy.XXX)
.doOnNext({storage.saveAllItemsLists(it)})
.map {/*TODO: convert from ItemsResponse to List<Item>*/}
} else {
Flowable.just(it)
}
})
The code emits changes to the favorite items. If there are 0 favorite items it will call the getAllFavItems()
function and insert the results into db (inserting the results will cause getAllFavitems()
to emit again).
Upvotes: 1
Reputation: 8386
Change to:
getAllFavItems().map { offers : ItemsResponse ->
storage.saveAllItemsLists(offers)
Single.just(storage.getAllFavItems())
}
You have twice {
and }
inside map
.
Upvotes: 0