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.