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.

  • 1
    I would suggest to remove .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
  • This is actually not a loop, at least not in the way you're describing. The request to the server is made in the main activity. The received data is read and the DB is updated. Then, the adapter for each RV simply reads the data from the DB and updates its contents. – coolDude Oct 4 at 16:05

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.