Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subscribeOn Android UI thread

I am new to RxJava and I think I am probably overlooking something very basic. So I have created an Observable that performs a long running I/O task like this. Now I want to make sure that subscriber receives its result on Android UI Thread:

Observable.create(new ObservableOnSubscribe<Device>() {
    @Override
    public void subscribe(@io.reactivex.annotations.NonNull final ObservableEmitter<Device> e) throws Exception {
        //Long running task
    }
}).subscribeOn(Schedulers.io())

As you can see, I have performed the long running task on Schedulers.io(). How do I switch back to Android UI thread?

like image 543
Dhruv Jagetiya Avatar asked Oct 06 '17 11:10

Dhruv Jagetiya


1 Answers

You have to perform observeOn(AndroidSchedulers.mainThread()).

like image 87
azizbekian Avatar answered Sep 29 '22 20:09

azizbekian