Skip to content

Instantly share code, notes, and snippets.

@prabirshrestha
Last active January 10, 2018 07:16
Show Gist options
  • Select an option

  • Save prabirshrestha/5632269 to your computer and use it in GitHub Desktop.

Select an option

Save prabirshrestha/5632269 to your computer and use it in GitHub Desktop.
RxJava Android scheduler
package rx.concurrency;
import android.os.Handler;
import android.os.Looper;
import rx.Scheduler;
import rx.Subscription;
import rx.concurrency.ExecutorScheduler;
import rx.util.AtomicObservableSubscription;
import rx.util.functions.Func2;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
/**
* Executes work on the Android UI thread.
* This scheduler should only be used to update the ui.
*/
public class AndroidScheduler extends Scheduler {
private static final AndroidScheduler INSTANCE = new AndroidScheduler();
public static AndroidScheduler getInstance() {
return INSTANCE;
}
private AndroidScheduler() {
}
@Override
public <T> Subscription schedule(final T state, final Func2<Scheduler, T, Subscription> action) {
final AtomicObservableSubscription subscription = new AtomicObservableSubscription();
final Scheduler _scheduler = this;
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
subscription.wrap(action.call(_scheduler, state));
}
});
return subscription;
}
@Override
public <T> Subscription schedule(T t, Func2<Scheduler, T, Subscription> schedulerTSubscriptionFunc2, long l, TimeUnit timeUnit) {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment