0

I am new to RXJAVA 2.

While learning it I reliazed that even I call observeOn (also not)on different thread using .observeOn(Schedulers.newThread()) I can still change text of textview.

I am really confused how it can happen even though I dont call .observeOn(AndroidSchedulers.mainThread())

Do you not think I should get an exception saying I can not update UI without calling main thread?

My Code :

public class TestData {

    public static void getData(TextView txt){
        List<UserInfo> mUSerInfoList=new ArrayList<>();

        UserInfo userInfo1=new UserInfo();
        userInfo1.setName("X");

        UserInfo userInfo2=new UserInfo();
        userInfo2.setName("Y");

        UserInfo userInfo3=new UserInfo();
        userInfo3.setName("Z");

        UserInfo userInfo4=new UserInfo();
        userInfo4.setName("A");

        UserInfo userInfo5=new UserInfo();
        userInfo5.setName("B");

        mUSerInfoList.add(userInfo1);
        mUSerInfoList.add(userInfo2);
        mUSerInfoList.add(userInfo3);
        mUSerInfoList.add(userInfo4);
        mUSerInfoList.add(userInfo5);

        Observer<UserInfo> observer=new Observer<UserInfo>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(UserInfo userInfo) {

              txt.setText(Thread.currentThread().getName());

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };

        Observable.fromIterable(mUSerInfoList)
            .subscribeOn(Schedulers.newThread())
            .observeOn(Schedulers.newThread())
            .subscribe(observer);
    }
}

and I call those on MainActivity

TextView txt=findViewById(R.id.txtView);
TestData.getData(txt);
Komal12
  • 3,340
  • 4
  • 16
  • 25
Eren
  • 11
  • 2
  • You should be getting an exception: https://stackoverflow.com/questions/9884246/how-to-set-text-of-text-view-in-another-thread . Please verify the schedulers are not overridden with RxJavaPlugins and that you are actually executing the code you showed (you could be executing an older, non-Rx version of it for example). – akarnokd Feb 16 '18 at 11:52
  • It is supposed to give an exception but it is not. I use those...... compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.0.8' – Eren Feb 16 '18 at 12:14

0 Answers0