Skip to content

Instantly share code, notes, and snippets.

@emrekose26
Forked from lapastillaroja/README.md
Last active March 14, 2017 11:43
Show Gist options
  • Select an option

  • Save emrekose26/8083221f13c20c984ef8cb75115effa9 to your computer and use it in GitHub Desktop.

Select an option

Save emrekose26/8083221f13c20c984ef8cb75115effa9 to your computer and use it in GitHub Desktop.

Revisions

  1. emrekose26 renamed this gist Mar 14, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. emrekose26 revised this gist Mar 14, 2017. 3 changed files with 2 additions and 2 deletions.
    File renamed without changes.
    2 changes: 1 addition & 1 deletion RxUtils.java → RxTransformer.java
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    import rx.android.schedulers.AndroidSchedulers;
    import rx.schedulers.Schedulers;

    public class RxUtil {
    public class RxTransformer {
    /**
    * {@link rx.Observable.Transformer} that transforms the source observable to subscribe in the
    * io thread and observe on the Android's UI thread.
    2 changes: 1 addition & 1 deletion UseExample.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ApiService.getInstance().apiCall(parameter)
    .compose(RxUtil.applyIOToMainThreadSchedulers())
    .compose(RxTransformer.applyIOToMainThreadSchedulers())
    .subscribe(data -> {});

  3. @lapastillaroja lapastillaroja renamed this gist Dec 3, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @lapastillaroja lapastillaroja revised this gist Dec 3, 2015. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions README.me
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    Problem
    ```
    Error:(29, 37) error: cannot find symbol method subscribeOn(Scheduler)
    ```

    This happens compiling this code borrowed from http://blog.danlew.net/2015/03/02/dont-break-the-chain/#comment-2388677116

    ```java
    final Observable.Transformer schedulersTransformer =
    observable -> observable.subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread());
    ```

    Environment
    - Android Studio 2.0
    - Retrolambda
    - Compiled with Java 8
  5. @lapastillaroja lapastillaroja created this gist Dec 2, 2015.
    35 changes: 35 additions & 0 deletions RxUtils.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import rx.Observable;
    import rx.Subscription;
    import rx.android.schedulers.AndroidSchedulers;
    import rx.schedulers.Schedulers;

    public class RxUtil {
    /**
    * {@link rx.Observable.Transformer} that transforms the source observable to subscribe in the
    * io thread and observe on the Android's UI thread.
    */
    private static Observable.Transformer ioToMainThreadSchedulerTransformer;

    static {
    ioToMainThreadSchedulerTransformer = createIOToMainThreadScheduler();
    }

    /**
    * Get {@link rx.Observable.Transformer} that transforms the source observable to subscribe in
    * the io thread and observe on the Android's UI thread.
    *
    * Because it doesn't interact with the emitted items it's safe ignore the unchecked casts.
    *
    * @return {@link rx.Observable.Transformer}
    */
    @SuppressWarnings("unchecked")
    private static <T> Observable.Transformer<T, T> createIOToMainThreadScheduler() {
    return tObservable -> tObservable.subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread());
    }

    @SuppressWarnings("unchecked")
    public static <T> Observable.Transformer<T, T> applyIOToMainThreadSchedulers() {
    return ioToMainThreadSchedulerTransformer;
    }
    }
    4 changes: 4 additions & 0 deletions UseExample.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    ApiService.getInstance().apiCall(parameter)
    .compose(RxUtil.applyIOToMainThreadSchedulers())
    .subscribe(data -> {});