Hello, I have a question on the use of Schedulers in the process, the operation of the UI in Android development, should be the main thread of execution, why I use the observeOn (Schedulers.io ()), TextView can still assignment, which is how to do, please help me to answer, very grateful.Code is as follows:
myTextView.setText("thread 1 id=" + Thread.currentThread().getId());
Observable.just(1, 2, 3, 4)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.newThread())
.map(new Func1<Integer, String>() {
@Override
public String call(Integer integer) {
myTextView.setText(myTextView.getText() + "\n" + "thread 2 id =" +
Thread.currentThread().getId());
return "5" + integer;
}
})
.observeOn(Schedulers.io())
.map(new Func1<String, Integer>() {
@Override
public Integer call(String s) {
myTextView.setText(myTextView.getText() + "\n" + "thread 3 id =" +
Thread.currentThread().getId());
return Integer.parseInt(s);
}
});
Results : TextView normal display
You didn't schedule on the proper thread. You have to use observeOn(AndroidSchedulers.mainThread()) so setText runs on the main thread.
First of all thank you for your answer, But I mean that I did not use observeOn (AndroidSchedulers.mainThread ()), however, still setText assignment success. Under normal situation, the program should throw an exception and fail the assignment. But using observeOn (Schedulers.io ()) did not appear abnormal.
I'm not an Android expert so I don't know why the setText didn't throw on an off-main-thread.
Anyway thank you very much.
The results show that the myTextView:
This is very weird,But I can not find the reason, o~
@YongHuiLuo link some sample code that exhibits this, only way for anyone to really debug it. Also I think you should post this in RxAndroid, as it's a little too specific for RxJava by itself unless there's a problem in observeOn
All view code does not throw if accessed off the main thread. Most calls
silently fail.
On Wed, Jan 6, 2016, 8:10 PM Zac Sweers [email protected] wrote:
@YongHuiLuo https://github.com/YongHuiLuo link some sample code that
exhibits this, only way for anyone to really debug it. Also I think you
should post this in RxAndroid, as it's a little too specific for RxJava by
itself unless there's a problem in observeOn—
Reply to this email directly or view it on GitHub
https://github.com/ReactiveX/RxJava/issues/3605#issuecomment-169514634.
@hzsweers
I'm very sorry, I've realized that I should post this in RxAndroid. Also, I should submit a complete code on my question. I will note this in the future.
@JakeWharton
Privately, my colleagues and I call you to Jake Great God, thank you for answering my questions. However, I would like to know more about the related information,For example, already silently fail. Why
Did you run your code in onCreate method? It has no relationship with Rxjava. You can create a new thread to set the text and run it in onCreate method and it will also work.
class TestThread extends Thread{
public void run() {
myTextView.setText(myTextView.getText() + "\n" + "thread 2 id =" +
Thread.currentThread().getId());
}
}
The reason why it can work need further study.
@Chaoba
I don't know the reason for you said.And I try write the code as you write,but it is wrong.
Who can tell me the reason ? Thanks.
I don't know the reason for you said.And I try write the code as you write,but it is wrong.
Who can tell me the reason ? Thanks.
Did you run the thread in onCreate() method? It works ok for me and will not work if you run it in onResume etc.
Any clue about the reason why it is work on onCreate method?
Most helpful comment
You didn't schedule on the proper thread. You have to use
observeOn(AndroidSchedulers.mainThread())sosetTextruns on the main thread.