question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

myTextView.setText () can be performed in a non-UI thread, Why

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
akarnokdcommented, Jan 6, 2016

You didn’t schedule on the proper thread. You have to use observeOn(AndroidSchedulers.mainThread()) so setText runs on the main thread.

0reactions
EhsanMashhadicommented, Mar 28, 2019

Any clue about the reason why it is work on onCreate method?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found