在 RXJava 调用之前未显示进度对话框 [英] Progress Dialog not showing up before RXJava call

查看:55
本文介绍了在 RXJava 调用之前未显示进度对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,在我执行长时间运行的任务之前,我启动了一个环形对话框,在 RxJava2 中完成.问题是对话框没有显示,我认为我没有阻塞主 UI 线程.

I have the following code, where I start a ring dialog before I make a long running task, done in RxJava2. The problem is that the dialog isn't showing up and I don't think I'm blocking the main UI thread.

  fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (ringProgressDialog != null) {
                if (ringProgressDialog.isShowing()) {
                    ringProgressDialog.dismiss();
                }
            }
            ringProgressDialog = ProgressDialog.show(SendConversationsActivity.this,
                    getResources().getString(R.string.creating_document_progress_dialog_title),
                    getResources().getString(R.string.conversation_progress_dialog_text),
                    true, false);
            FileNameAndContacts filenameAndContacts = new FileNameAndContacts();
            if (tvNoDatSelected.getVisibility() != View.VISIBLE) {
                filenameAndContacts.setFileName("");
            }
            createDocument(filenameAndContacts)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnError(throwable -> Timber.e(throwable, "Error in getting document"))
                    .subscribe(fileAndContacts -> {
                        if (ringProgressDialog.isShowing()) { //debugger says 
                                                              //dialog is showing. 
                            ringProgressDialog.dismiss();
                        }
                        sendDocumentInEmail(fileAndContacts);

                    });
     }

任务正常执行.在我在同一个活动中执行另一个 RXJava 任务之前,我还显示了另一个环进度对话框,这个对话框出现了.

Task executes properly. I also show another ring progress dialog before I do another RXJava task in the same activity, this one shows up.

如果我注释掉 RxJava 调用,就会出现对话框.所以 RxJava 调用中的某些东西是阻塞的.

If I comment out the RxJava call, the dialog shows up. So something in the RxJava call is blocking.

//编辑这个简单的 observable 也阻止了进度对话框的显示(但 Toast 显示):

//EDIT This simple observable also blocks the progress dialog from showing (but the Toast displays):

                Observable.just("Hello, world")
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnSubscribe(disposable -> {
                        Toast.makeText(SendConversationsActivity.this, "Toast...", Toast.LENGTH_SHORT).show();
                        //ringProgressDialog.show();
                    })
                    .doOnTerminate(() -> {
                        //ringProgressDialog.dismiss();
                    })
                    .subscribe(s -> {
                        //Toast.makeText(SendConversationsActivity.this, s, Toast.LENGTH_SHORT).show();
                        ringProgressDialog.dismiss();
                    });

推荐答案

createDocument 是如何实现的?创建,来自Callable

How is createDocument implemented? create, fromCallable

@akarnokd 我做计算然后做一个 Single.just(fileNameAndContacts)

@akarnokd I do the calculations then do a Single.just(fileNameAndContacts)

正如怀疑的那样,您在当前线程(主线程)上计算文档并阻塞它.您应该将其移动到 fromCallable 中,当与 subscribeOn(Schedulers.io()) 结合使用时,它将在后台进行计算:

As suspected, you compute the document on the current thread (main) and blocking it. You should move it into fromCallable and it will compute in the background when combined with subscribeOn(Schedulers.io()):

Observable.fromCallable(() -> {
     /* compute the document here */
    return fileNameAndContacts;
}); 

这篇关于在 RXJava 调用之前未显示进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆