Last active
January 10, 2018 07:16
-
-
Save prabirshrestha/5632269 to your computer and use it in GitHub Desktop.
RxJava Android scheduler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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