I am accessing database using Maybe and my goal is to call the network if Maybe will complete indicating that there is no data in database (I am using room). I have following functions:
@Override
public Observable<List<Item>> getResults() {
return getResultsFromDatabase().toObservable().switchIfEmpty(getResultsFromNetwork());
}
@Override
public Observable<List<Item>> getResultsFromNetwork() {
System.out.println("getting results from network");
return api.getData().doOnNext(new Consumer<List<Item>>() {
@Override
public void accept(List<Item> items) throws Exception {
itemDao.insert(items);
}
});
}
@Override
public Maybe<List<Item>> getResultsFromDatabase() {
System.out.println("getting coins from database");
return itemDao.getAllItems();
}
However, the switchIfEmpty works a little unexpectedly, as it is always called, even though there are objects in database.
return getResultsFromDatabase().toObservable().switchIfEmpty(getResultsFromNetwork());
- Is it possible to transform Maybe into Observable using only toObservable()?
- Is there another way to check if Maybe will be empty and then act upon it?
onComplete()
when there are no rows? – Christabella