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);