RxJava在相同的阻塞UI线程上运行,并且不显示AlertDialog

时间:2017-10-09 07:32:15

标签: android multithreading rx-java

我正在尝试使用RxJava在加载某些方法时显示AlertDialog。它不起作用,UI被阻塞2秒,当使用Debugger单步执行时,调试器显示它在UI线程上运行。我已经添加了Schedulers.IO,所以我做错了什么?

boolean initialize() {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
    }
    return true;
}

public AlertDialog showSomePopup(Context context, String msg) {
    return new AlertDialog.Builder(context)
            .setTitle("Loading...")
            .setMessage(msg)
            .setPositiveButton("Ok", null)
            .show();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final AlertDialog dialog = showSomePopup(this, "Waiting ..");

    Single.just(initialize())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<Boolean>() {
        @Override
        public void accept(@NonNull Boolean aBoolean) throws Exception {
            dialog.dismiss();
        }
    });
}

5 个答案:

答案 0 :(得分:2)

问题是.subscribe()方法在initialize()方法没有发出之前(即你正在使用.just()时才会被调用,直到{{1}没有回来。

答案 1 :(得分:1)

您的初始化函数应返回调用者可以订阅的Observable。在您的情况下,通过调用initialize()然后等待结果返回来启动序列。你应该做什么:

Single<Boolean> initialize() {
    return Single.fromCallable(new Callable<Boolean>() {
       @Override
        public Boolean call() throws Exception {
            try {
                Thread.sleep(2000);
                return true;
            } catch (Exception ex) {
                return false;
            }
        }
    });
}

现在你可以输入你喜欢的代码:

initialize()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Consumer<Boolean>() {
        @Override
        public void accept(@io.reactivex.annotations.NonNull Boolean aBoolean) throws Exception {
            if(aBoolean == true) {
                dialog.dismiss();
            }
        }
    });

它会按照您的意愿运作。

答案 2 :(得分:1)

作为@Xavier Rubio Jansana的后续答案正确答案。 对于完成或失败的方法,请使用完整

Completable.fromAction(() -> {
        init();
    })
            .subscribeOn(Schedulers.io())
            //.observeOn() AndroidScheduler
            .subscribe(() -> {
               // dismiss action
});

private void init() throws Exception {
    Thread.sleep(1000);
}

答案 3 :(得分:1)

这是正确答案:

Single<Boolean> initialize() {
    return Single.create(new SingleOnSubscribe<Boolean>() {
        @Override
        public void subscribe(@NonNull SingleEmitter<Boolean> e) throws Exception {
            e.onSuccess(true);
        }
    });
}

public AlertDialog showSomePopup(Context context, String msg) {
    return new AlertDialog.Builder(context)
            .setTitle("Loading...")
            .setMessage(msg)
            .setPositiveButton("Ok", null).create();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final AlertDialog dialog = showSomePopup(this, "lol");
    dialog.show();

    initialize().subscribeOn(Schedulers.io())
            .doOnSuccess(new Consumer<Boolean>() {
                @Override
                public void accept(@NonNull Boolean aBoolean) throws Exception {
                    dialog.dismiss();
                }
            })
            .subscribe();
}

答案 4 :(得分:1)

在创建AlertDialog()时缺少AlertDialog create()。在显示之前添加它。

return new AlertDialog.Builder(context)
        .setTitle("Loading...")
        .setMessage(msg)
        .setPositiveButton("Ok", null)
        .create()
        .show();