This is the first time that I'm using RxJava (as well as Android Room, although not as relevant to the problem). I have nested RecyclerViews in my app, where the first RV shows the day and the second RV shows the forecast for the day. The app allows the user to change their zip code. Once the zip code is changed, the data within the 2 RVs should be updated. Therefore, a new network request is made and the 2 RVs should update their data.
Again, this is my first time using RxJava. Therefore, I really don't know what the best observer/ovservable combo is best for my particular situation. I figured that a Flowable would work best because it will emit data each time the data changes, which is exactly what happens when the end user changes their zip code: a new network request is made and the Room DB is updated.
However, ever since I've updated my Dao to using Flowable instead of Single, the data is updated within the DB (I've checked using Stetho) but the app now has some serious jank to it: it looks like it is constantly trying to refresh the data and the screen is constantly refreshing.
Here's how I update the data within the first RV (which is contained inside of a fragment):
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// A bunch of other initial setup
Flowable<List<LocalDate>> source = MyApp.getDatabase(this.getContext()).getDao().getUniqueDays();
source.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(uniqueDays -> {
mAdapter.refreshData(uniqueDays);
});
}
I follow the same pattern for the data that is within the second RV (which is nested within the first RV, so this is called within the first RV's adapter's onBindViewHolder()):
public void bind(LocalDate date, int position){
Flowable<List<ForecastCondition>> source = MyApp.getDatabase(mContext).getDao().getForecastForDay(date);
source.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.delaySubscription(5, TimeUnit.SECONDS)
.subscribe(conditions -> mAdapter.refreshData(conditions));
}
I had to add the delaySubscription(5, TimeUnit.SECONDS) because the forecasts weren't always showing up within the second RV, but they do with the delay.
I know that I'm definitely making some mistakes here, but I don't exactly know what. Again, I've never worked with this before so any assistance would be greatly appreciated. I've read that I should be using Disposable for my subscription, but I don't completely understand what that does nor how it may solve my problem. The bigger problem though is that I'm unsure of how to solve the jank/stutter issue. Many thanks to anyone that can provide help on this. Your next tasty, cold (virtual) beverage will be on me.
.delaySubscription(5, TimeUnit.SECONDS)and find out why the forecasts are not showing. In the present code a loop is possible because of the timeout:onBindViewHolder()->getDao().getForecastForDay(date)-> request to the server -> update of the first RecyclerView ->onBindViewHolder()– atarasenko Oct 4 at 15:57