RxJava. How to display progressDialog in Observable?


I have a method for an example, I need to display progressDialog when receiving a request, in onCompleted()dismiss in onNext show , but as a result, progressDialog not displayed on the screen. What could be the reason, most likely I'm misunderstanding something?

public void newsGetRxEnd(RecyclerView mRecyclerView, Context context) {
          progressDialog = new ProgressDialog(context, R.style.MyTheme);

    Link link = retrofit.create(Link.class);
        Observable<List<News>> observable = link.endNews(db.getMin("news", "idNews"));

                .subscribe(new Observer<List<News>>() {
                    public void onCompleted() {
                        Log.d(LOG_TAG, "onCompleted");

                    public void onError(Throwable e) {
                        Log.d(LOG_TAG, "onError " + e);

                    public void onNext(List<News> newses) {
                        Log.d(LOG_TAG, "onNext " + newses.size());

                        for (int i = 0; i < newses.size(); i++) {
                            db.addRec(newses.get(i).getTitle(), newses.get(i).getId(), newses.get(i).getSummary(),
                            Log.d(LOG_TAG, "db.getNewsList().size(): " + db.getNewsList().size());





Most likely, the dialogue with progress simply does not have time to appear.

You now have this:

  1. Loading from the network in a background thread
  2. Upon its completion, the main thread is written to the database.
  3. After recording, a dialogue is shown.
  4. Immediately after that, onCompleted() is called and the dialog is hidden.

Because most likely, only a few milliseconds pass between n3 and n4, then the dialogue simply does not have time to be displayed.

I can offer several options.

  1. Show dialog when starting a request to the network.
  2. Show dialog before writing to the database. If it takes a significant amount of time, then the dialogue will have time to appear. But here the point is that it is better to write to the database, too, in a background thread, and not in the main one, tk. the interface will freeze and generally ANR may occur.

In both cases, you need to handle errors and hide the dialog in onError .

And we must remember that if you write to the database in a background thread and show a dialog before it, then calls to it must be made (as well as to the rest of the UI) in the main thread.

Scroll to Top