myTextView.setText () can be performed in a non-UI thread, Why
See original GitHub issueHello, 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
Issue Analytics
- State:
- Created 8 years ago
- Comments:13 (4 by maintainers)
Top Results From Across the Web
No error calling textView.setText from non UI-Thread. Why?
1 Answer 1 · Actually, if you try to modify a View outside of the main thread it will throw an exception. –...
Read more >The Main Application Thread - CommonsWare
When you call setText() on a TextView , you probably think that the screen is updated with the text you supply, right then...
Read more >TextView - Android Developers
android:bufferType, Determines the minimum type that getText() will return. ... android:textIsSelectable, Indicates that the content of a non-editable text ...
Read more >Android Threading: All You Need to Know - Toptal
For this reason, they are usually performed in separate threads, which thereby avoids blocking the UI while they are being performed (i.e., they...
Read more >Why Your TextView or Button Text Is Not Updating - Tek Eye
The setText call posts a message to update the display, so the update does not happen immediately. Once remaining app code has run...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You didn’t schedule on the proper thread. You have to use
observeOn(AndroidSchedulers.mainThread())
sosetText
runs on the main thread.Any clue about the reason why it is work on onCreate method?