可观察运行在主线程上,即使在另一个线程上调用了 subscribeOn()
2022-09-01 21:57:37
我在一次活动中遇到了一个奇怪的问题。从拍摄照片/视频回来时,在我的照片中,我显示了一个对话框,允许用户命名相机。用户按“确定”后,我将发送到具有请求文件名的使用者,该主题将复制文件(并显示进度对话框)。onActivityResult
onNext()
由于某种原因,执行复制的函数总是在主线程上调用,即使我调用 .map()
subscribeOn(Schedulers.io())
@Override
protected void onActivityResult(final int requestCode, int resultCode, Intent intent) {
...
final PublishSubject<String> subject = PublishSubject.create();`
mSubscription = subject
.subscribeOn(Schedulers.io())
.map(new Func1<String, String>() {
@Override
public String call(String fileName) {
Log.I.d(TAG,"map");
return doSomeIOHeavyFuncition();
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<String>() {
@Override
public void call(final String fullPath) {
Log.d(TAG,"onNext");
doSomethingOnUI(fullPath);
subject.onCompleted();
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
...
}
}, new Action0() {
@Override
public void call() {
...
}
});
final AlertDialog dialog = new AlertDialog.Builder
....
.create()
.show();
dialog.getButton(DialogInterface.BUTTON_POSITIVE)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String someString = getStringFromDialog(dialog);
dialog.dismiss();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
showProgressDialog();
subject.onNext(someString);
}
});
}
更改呼叫以解决问题。我仍然想知道为什么它不起作用...subscribeOn(Schedulers.io())
observeOn(Schedulers.io())