-
-
Save pyadav/72abfcd19a3f0ee29044f331139a1d6f to your computer and use it in GitHub Desktop.
Helpful RxJava functions for daily use
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
| /** | |
| * Transforms an observable to run on a new thread | |
| * and to be observed on Androids main thread. | |
| */ | |
| public static <T> Observable.Transformer<T, T> androidAsync() { | |
| return new Observable.Transformer<T, T>() { | |
| @Override | |
| public Observable<T> call(Observable<T> observable) { | |
| return observable | |
| .subscribeOn(Schedulers.newThread()) | |
| .observeOn(AndroidSchedulers.mainThread()); | |
| } | |
| }; | |
| } |
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
| /** | |
| * Provides an equivalent of mapWithIndex operation. | |
| * | |
| * Usage: | |
| * List<String> names = Arrays.asList("Joe", "Bob", "Dave"); | |
| * mapWithIndex(names, new Func2<String, Integer, String>() { | |
| * @Override | |
| * public String call(String name, Integer index) { | |
| * if (index > 0) { | |
| * return name + " !!"; | |
| * } else { | |
| * return name; | |
| * } | |
| * } | |
| * }); | |
| */ | |
| public static <T,R> Observable<R> mapWithIndex(Collection<T> collection, Func2<T, Integer, R> mapFunc) { | |
| return Observable.from(collection).zipWith(Observable.range(0, collection.size()), mapFunc); | |
| } |
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
| /** | |
| * Throws an exception; for testing purposes. | |
| */ | |
| public static <T> Observable<T> throwsException() { | |
| return Observable.create(new Observable.OnSubscribe<T>() { | |
| @Override | |
| public void call(Subscriber<? super T> subscriber) { | |
| subscriber.onError(new Exception("Mock exception")); | |
| } | |
| }); | |
| } |
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
| /** | |
| * Unwraps a list of items and emits them one by one. | |
| */ | |
| public static <T> Func1<List<T>, Observable<T>> unwrapList() { | |
| return new Func1<List<T>, Observable<T>>() { | |
| @Override | |
| public Observable<T> call(List<T> list) { | |
| return Observable.from(list); | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment