Back to project page android-javarx-example.
The source code is released under:
Apache License
If you think the Android project android-javarx-example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.bnsantos.movies.providers; /* w w w .jav a 2s .c om*/ import android.util.Log; import com.bnsantos.movies.model.Movie; import java.util.List; import rx.Observable; import rx.Subscription; import rx.functions.Action0; import rx.functions.Action1; import rx.schedulers.Schedulers; import rx.subjects.BehaviorSubject; /** * Created by bruno on 19/11/14. */ public class SubjectMovieProvider extends MovieProvider { private final BehaviorSubject<List<Movie>> mMoviesSubject; private final Subscription mServerSubscription; private final Subscription mCacheSubscription; public SubjectMovieProvider() { mMoviesSubject = BehaviorSubject.create(); mServerSubscription = cacheAndRetrieveMovies().subscribeOn(Schedulers.newThread()).subscribe(new Action1<List<Movie>>() { @Override public void call(List<Movie> movieList) { Log.d(TAG, "ServerObservable received movies"); mMoviesSubject.onNext(movieList); } }, new Action1<Throwable>() { @Override public void call(Throwable throwable) { Log.e(TAG, "Fetching movies from server error", throwable); mMoviesSubject.onError(throwable); } }, new Action0() { @Override public void call() { Log.d(TAG, "ServerObservable completed"); mMoviesSubject.onCompleted(); } }); mCacheSubscription = retrieveCachedMovies().subscribeOn(Schedulers.newThread()).subscribe(new Action1<List<Movie>>() { @Override public void call(List<Movie> movieList) { Log.d(TAG, "CacheObservable received movies"); mMoviesSubject.onNext(movieList); } }, new Action1<Throwable>() { @Override public void call(Throwable throwable) { Log.e(TAG, "Fetching movies from cache error", throwable); mMoviesSubject.onError(throwable); } }, new Action0() { @Override public void call() { Log.d(TAG, "CacheObservable completed"); mMoviesSubject.onCompleted(); } }); } public void unSubscribe() { mCacheSubscription.unsubscribe(); mServerSubscription.unsubscribe(); } public Observable<List<Movie>> subscribe() { return mMoviesSubject; } }