I need to fetch data from DB. If it's empty, get data from server and insert it into database. Here is my code,
public Flowable<List<Data>> test() {
return dataDao.allDatas()
.switchIfEmpty(datas())
.doOnNext(datas -> userStoreDao.insert(datas))
);
}
public Flowable<List<Data>> datas() {
return Flowable.zip(userFavoriteDatas(), userOtherDatas(),
(favoriteDatas, otherDatas) -> {
favoriteDatas.addAll(otherDatas);
return favoriteDatas;
});
}
public Flowable<List<Data>> userFavoriteDatas() {
return userDatas()
.map(UserDatas::favoriteDatas)
.flatMapIterable(datas-> datas)
.map(aLong -> new UserData(aLong, 1))
.toList()
.toFlowable();
}
public Flowable<List<Data>> userOtherDatas() {
return userDatas()
.map(UserDatas::otherDatas)
.flatMapIterable(datas-> datas)
.map(aLong -> new UserData(aLong, 1))
.toList()
.toFlowable();
}
private Flowable<Datas> userDatas() {
return api
.userDatas()
.toFlowable()
.share();
}
@GET("user/datas")
Single<Datas> datas();
It reaches second part when the first part returns empty result. datas() doesn't reached till the end while I'm running only the second part it works but combining with switchIfEmpty() it reaches to userDatas() and doesn't complete
I tried concat()
as well with the same result.
(From the comments):
If you use
toList
, that requires a finite stream. Based on the OP's feedback, theallDatas
source was infinite but returned an emptyList
. The resolution was to apply at leasttake(1)
to get exactly one response fromallDatas
and then optionally filter out the emptyList
soswitchIfEmpty
can switch over to the alternatives: