Skip to content Skip to sidebar Skip to footer

How To Use Switchifempty?

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> test() { return dataDao.

Solution 1:

(From the comments):

If you use toList, that requires a finite stream. Based on the OP's feedback, the allDatas source was infinite but returned an empty List. The resolution was to apply at least take(1) to get exactly one response from allDatas and then optionally filter out the empty List so switchIfEmpty can switch over to the alternatives:

return allDatas.take(1).filter(list -> !list.isEmpty())
       .switchIfEmpty(datas())
       .doOnNext(datas -> userStoreDao.insert(datas));

Post a Comment for "How To Use Switchifempty?"