I am having difficulty understanding how Flowable
works in Room. I have Dao
methods like this
@Insert(onConflict = OnConflictStrategy.REPLACE)
void upsert(List<Site> sites);
@Query("SELECT * FROM site ORDER BY distance ASC")
Flowable<List<Site>> getSites();
I expect that whenever I call upsert
subscriber to Flowable
object returned by getSites()
will always be called. Is my assumption true?
Here is how I am susbcribing to this flowable
private final Flowable<List<Site>> siteFlowable;
ApiService apiService;
FuelDatabase database;
@Override
public void getSites(boolean showOnlyKeySites) {
// add sites from cache first, then fetch network -> update cache -> update ui
disposable = siteFlowable.flatMap(Flowable::fromIterable)
.filter(site -> site.isValid())
.buffer(100, TimeUnit.MILLISECONDS, 20)
.takeUntil(sites -> sites.size() == 0)
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(mapView::addPins)
.subscribe(sites -> {
Timber.d("Flowable emitted %d items", sites.size());
}, Timber::e);
apiService.getSites()
.map(SiteListResponse::getData)
.flatMap(Observable::fromIterable)
.filter(Site::isValidSite)
.toList().toObservable()
.subscribe(sites -> {
Timber.i("Success Fetching %d sites", sites.size());
database.siteDao().clear();
database.siteDao().upsert(sites);
}, throwable -> Timber.e(throwable, "Error fetching sites"));
}
This flowable doesn't get called after upsert()
is called. API is returning valid data and data is getting entered into database.