Inoshichi
Inoshichi

Reputation: 33

execute subscriber in new thread rxandroid

I'm currently trying to implement rxandroid in my app to update an adapter. The only problem is that when i call the adapter.notiyDataSetChanged() the app freezes until the method has finished.

this is what i've done so far:

private Subscriber<? super Object> subscriber = new Subscriber<Object>() {

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onNext(Object o) {

           adapter.notifyDataSetChanged();


        }
    }
};

...

and then i do the subscribe inside the onResume

@Override
public void onResume() {
    super.onResume();
    MObservable.getInstance().getObservable()
            .delay(2000,TimeUnit.MILLISECONDS,Schedulers.trampoline())
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(subscriber);
}

and the unsubscribe inside onPause.

The events are triggered in a service that stars the process various times per minute.

So finally my question is: is there any way to not freeze the app while the notify method is called? is the notify executing on a new thread in my code or the subscribeOn is still making notify running on the main thread (as i suspect).

Thank you in advance

Upvotes: 0

Views: 207

Answers (1)

R. Zag&#243;rski
R. Zag&#243;rski

Reputation: 20268

Probably, the problem is with Schedulers.trampoline() fragment.

From documentation of Schedulers.trampoline():

Creates and returns a Scheduler that queues work on the current thread to be executed after the current work completes.

On Android it means the current Thread from main Thread.

Remove this parameters and see how Observable behaves.

Upvotes: 0

Related Questions