How to Manage State with RxJava in Android using Java (Not Kotlin) - android
I am attempting to developed an Android application based on the following talk presented by Jake Wharton
The State of Managing State with RxJava
21 March 2017 – Devoxx (San Jose, CA, USA)
Jake promised a part 2 and/or GITHUB example which I am unable to find (If indeed either exists)
At a high level I can follow/understand the majority of the above talk.
However I have the following questions.
I can see how employing UiEvent, UiModel, Action, and Result keeps concerns separated.
What I am confused about is the following:-
The diagram on slide 194 shows the "flow/stream" of Observables as
Android Device -----> Observable<UiEvent> -----> <application code> -----> Observable<Action> -----> {Backend}
{Backend} -----> Observable<Result> -----> <application code> -----> Observable<UiModel> -----> Android Device
Slide 210 contains this code snippet, showing how the Result(s) stream is "scan"ned into UiModel
SubmitUiModel initialState = SubmitUiModel.idle();
Observable<Result> results = /* ... */;
Observable<SubmitUiModel> uiModels = results.scan(initialState, (state, result) -> {
if (result == CheckNameResult.IN_FLIGHT
|| result == SubmitResult.IN_FLIGHT)
return SubmitUiModel.inProgress();
if (result == CheckNameResult.SUCCESS)
return SubmitUiModel.idle();
if (result == SubmitResult.SUCCESS)
return SubmitUiModel.success();
// TODO handle check name and submit failures...
throw new IllegalArgumentException("Unknown result: " + result);
});
and the final code snippet on slide 215, the code snippet resembles this:-
ObservableTransformer<SubmitAction, SubmitResult> submit =
actions -> actions.flatMap(action -> service.setName(action.name)
.map(response -> SubmitResult.SUCCESS)
.onErrorReturn(t -> SubmitResult.failure(t.getMessage()))
.observeOn(AndroidSchedulers.mainThread())
.startWith(SubmitResult.IN_FLIGHT));
ObservableTransformer<CheckNameAction, CheckNameResult> checkName =
actions -> actions.switchMap(action -> action
.delay(200, MILLISECONDS, AndroidSchedulers.mainThread())
.flatMap(action -> service.checkName(action.name))
.map(response -> CheckNameResult.SUCCESS)
.onErrorReturn(t -> CheckNameResult.failure(t.getMessage()))
.observeOn(AndroidSchedulers.mainThread())
.startWith(CheckNameResult.IN_FLIGHT));
which illustrates conversion from Action(s) to Result(s)
what am I missing from this talk/slide-deck on how to combine the UiEvent/UiModel to the Action/Result stream?
The stream is driven by UiEvents
How do you complete the flow from UiEvent(s) to Action back to Result then finally UiModel?
UPDATE
Using the Star Wars API I have taken the following approach
I use my UI Events to drive the transformation between UI Events to Results via Actions, then scan the results to map back to UI Model.
Heres my classes and code:-
ACTION CLASSES
==============
public abstract class Action<T> {
Api service = Service.instance();
final T data;
public Action(final T data) {
this.data = data;
}
public T getData() {
return data;
}
public abstract Observable<Response<String>> execute();
}
public class CheckCharacterAction extends Action<String> {
public CheckCharacterAction(final String characterName) {
super(characterName);
}
#Override
public Observable<Response<String>> execute() {
return service.peopleSearch(getData());
}
}
public class CheckFilmAction extends Action<String> {
public CheckFilmAction(final String filmTitle) {
super(filmTitle);
}
#Override
public Observable<Response<String>> execute() {
return service.filmSearch(getData());
}
}
public class SearchAction extends Action<String> {
public SearchAction(final String search) {
super(search);
}
#Override
public Observable<Response<String>> execute() {
return service.filmSearch(getData());
}
}
EVENT CLASSES
=============
public abstract class UiEvent<T> {
private final T data;
public UiEvent(final T data) {
this.data = data;
}
public T getData() {
return data;
}
}
public class CharacterUiEvent extends UiEvent<String> {
public CharacterUiEvent(final String name) {
super(name);
}
}
public class FilmUiEvent extends UiEvent<String> {
public FilmUiEvent(final String title) {
super(title);
}
}
public class SearchUiEvent extends UiEvent<String> {
public SearchUiEvent(final String data) {
super(data);
}
}
UI MODEL CLASSES
================
public class UiModel<T> {
public final boolean isProgress;
public final String message;
public final boolean isSuccess;
public T data;
public UiModel(final boolean isProgress) {
this.isProgress = isProgress;
this.message = null;
this.isSuccess = false;
this.data = null;
}
public UiModel(final T data) {
this.isProgress = false;
this.message = null;
this.isSuccess = true;
this.data = data;
}
public UiModel(final String message) {
this.isProgress = false;
this.message = message;
this.isSuccess = false;
this.data = null;
}
public UiModel(final boolean isProgress, final String message, final boolean isSuccess, final T data) {
this.isProgress = isProgress;
this.message = message;
this.isSuccess = isSuccess;
this.data = data;
}
}
public class CharacterUiModel extends UiModel<JsonData> {
public CharacterUiModel(final boolean isProgress) {
super(isProgress);
}
public CharacterUiModel(final JsonData data) {
super(data);
}
public CharacterUiModel(final String message) {
super(message);
}
public CharacterUiModel(final boolean isProgress, final String message, final boolean isSuccess, final JsonData data) {
super(isProgress, message, isSuccess, data);
}
public static CharacterUiModel inProgress() {
return new CharacterUiModel(true);
}
public static CharacterUiModel success(final JsonData data) {
return new CharacterUiModel(data);
}
public static CharacterUiModel failure(final String message) {
return new CharacterUiModel(message);
}
}
public class FilmUiModel extends UiModel<JsonData> {
public FilmUiModel(final boolean isProgress) {
super(isProgress);
}
public FilmUiModel(final JsonData data) {
super(data);
}
public FilmUiModel(final String message) {
super(message);
}
public FilmUiModel(final boolean isProgress, final String message, final boolean isSuccess, final JsonData data) {
super(isProgress, message, isSuccess, data);
}
public static FilmUiModel inProgress() {
return new FilmUiModel(true);
}
public static FilmUiModel success(final JsonData data) {
return new FilmUiModel(data);
}
public static FilmUiModel failure(final String message) {
return new FilmUiModel(message);
}
}
public class SearchUiModel extends UiModel<JsonData> {
private SearchUiModel(final boolean isProgress) {
super(isProgress);
}
private SearchUiModel(final JsonData data) {
super(data);
}
private SearchUiModel(final String message) {
super(message);
}
private SearchUiModel(final boolean isProgress, final String message, final boolean isSuccess, final JsonData data) {
super(isProgress, message, isSuccess, data);
}
public static SearchUiModel idle() {
return new SearchUiModel(false, null, false, null);
}
public static SearchUiModel inProgress() {
return new SearchUiModel(true);
}
public static SearchUiModel success(final JsonData data) {
return new SearchUiModel(data);
}
public static SearchUiModel failure(final String message) {
return new SearchUiModel(message);
}
}
RESULT CLASSES
==============
public abstract class Result<T> {
public enum LIFECYCLE {
DEPARTURE_LOUNGE,
IN_FLIGHT,
LANDED_SAFELY,
CRASHED_BURNED
}
final LIFECYCLE lifecycle;
final T data;
final String errorMessage;
public Result(final LIFECYCLE lifecycle, final T data, final String errorMessage) {
this.lifecycle = lifecycle;
this.data = data;
this.errorMessage = errorMessage;
}
public T getData() {
return data;
}
public String getErrorMessage() {
return errorMessage;
}
public LIFECYCLE getLifecycle() {
return lifecycle;
}
}
public class CharacterResult extends Result<JsonData> {
private CharacterResult(final LIFECYCLE lifecycle, final JsonData data, final String errorMessage) {
super(lifecycle, data, errorMessage);
}
private CharacterResult(final LIFECYCLE lifecycle) {
super(lifecycle, null, null);
}
public static CharacterResult departureLounge() {
return new CharacterResult(LIFECYCLE.DEPARTURE_LOUNGE);
}
public static CharacterResult inflight() {
return new CharacterResult(LIFECYCLE.IN_FLIGHT);
}
public static CharacterResult landedSafely(final JsonData data) {
return new CharacterResult(LIFECYCLE.LANDED_SAFELY, data, null);
}
public static CharacterResult crashedBurned(final String errorMessage) {
return new CharacterResult(LIFECYCLE.CRASHED_BURNED, null, errorMessage);
}
}
public class FilmResult extends Result<JsonData> {
private FilmResult(final LIFECYCLE lifecycle, final JsonData data, final String errorMessage) {
super(lifecycle, data, errorMessage);
}
private FilmResult(final LIFECYCLE lifecycle) {
super(lifecycle, null, null);
}
public static FilmResult departureLounge() {
return new FilmResult(LIFECYCLE.DEPARTURE_LOUNGE);
}
public static FilmResult inflight() {
return new FilmResult(LIFECYCLE.IN_FLIGHT);
}
public static FilmResult landedSafely(final JsonData data) {
return new FilmResult(LIFECYCLE.LANDED_SAFELY, data, null);
}
public static FilmResult crashedBurned(final String errorMessage) {
return new FilmResult(LIFECYCLE.CRASHED_BURNED, null, errorMessage);
}
}
public class SearchResult extends Result<JsonData> {
private SearchResult(final LIFECYCLE lifecycle, final JsonData data, final String errorMessage) {
super(lifecycle, data, errorMessage);
}
private SearchResult(final LIFECYCLE lifecycle) {
super(lifecycle, null, null);
}
public static SearchResult departureLounge() {
return new SearchResult(LIFECYCLE.DEPARTURE_LOUNGE);
}
public static SearchResult inflight() {
return new SearchResult(LIFECYCLE.IN_FLIGHT);
}
public static SearchResult landedSafely(final JsonData data) {
return new SearchResult(LIFECYCLE.LANDED_SAFELY, data, null);
}
public static SearchResult crashedBurned(final String errorMessage) {
return new SearchResult(LIFECYCLE.CRASHED_BURNED, null, errorMessage);
}
}
I then set up my Rx Streams as follows from my Activity onCreate() method:-
final Observable<SearchUiEvent> searchEvents = RxView.clicks(activityMainBinding.searchButton)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.map(ignored -> new SearchUiEvent(activityMainBinding.filmTitle.getText().toString()));
final Observable<FilmUiEvent> filmEvents = RxTextView.afterTextChangeEvents(activityMainBinding.filmTitle)
.skipInitialValue()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.delay(1000, MILLISECONDS, AndroidSchedulers.mainThread())
.map(text -> new FilmUiEvent(text.view().getText().toString()));
final Observable<CharacterUiEvent> characterEvents = RxTextView.afterTextChangeEvents(activityMainBinding.people)
.skipInitialValue()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.delay(200, MILLISECONDS, AndroidSchedulers.mainThread())
.map(text -> new CharacterUiEvent(text.view().getText().toString()));
/**
*
*/
final Observable<UiEvent> uiEvents = Observable.merge(searchEvents, filmEvents, characterEvents);
/*********
*
*/
final ObservableTransformer<SearchUiEvent, SearchResult> searchAction =
events -> events.flatMap(event -> new SearchAction(event.getData()).execute().subscribeOn(Schedulers.io()))
.map(response -> SearchResult.landedSafely(new JsonData(response.body())))
.onErrorReturn(throwable -> SearchResult.crashedBurned(throwable.getMessage()))
.startWith(SearchResult.inflight());
final ObservableTransformer<FilmUiEvent, FilmResult> filmAction =
events -> events.flatMap(event -> new CheckFilmAction(event.getData()).execute().subscribeOn(Schedulers.io()))
.map(response -> FilmResult.landedSafely(new JsonData(response.body())))
.onErrorReturn(throwable -> FilmResult.crashedBurned(throwable.getMessage()))
.startWith(FilmResult.inflight());
final ObservableTransformer<CharacterUiEvent, CharacterResult> characterAction =
events -> events.flatMap(event -> new CheckCharacterAction(event.getData()).execute().subscribeOn(Schedulers.io()))
.map(response -> CharacterResult.landedSafely(new JsonData(response.body())))
.onErrorReturn(throwable -> CharacterResult.crashedBurned(throwable.getMessage()))
.startWith(CharacterResult.inflight());
final ObservableTransformer<UiEvent, ? extends Result> whatever = events -> events.publish(shared -> Observable.merge(
shared.ofType(SearchUiEvent.class).compose(searchAction),
shared.ofType(CharacterUiEvent.class).compose(characterAction),
shared.ofType(FilmUiEvent.class).compose(filmAction)));
/**
*
*/
final UiModel initialState = SearchUiModel.idle();
final Observable<? extends Result> results = uiEvents.compose(whatever).doOnSubscribe(COMPOSITE_DISPOSABLE::add);
final Observable<UiModel> models = results.scan(initialState, (state, result) -> {
Log.e(TAG, "scan() state = " + state + " result = " + result);
if (result.getLifecycle().equals(SearchResult.LIFECYCLE.DEPARTURE_LOUNGE) ||
result.getLifecycle().equals(CharacterResult.LIFECYCLE.DEPARTURE_LOUNGE) ||
result.getLifecycle().equals(FilmResult.LIFECYCLE.DEPARTURE_LOUNGE)) {
return SearchUiModel.idle();
}
if (result.getLifecycle().equals(SearchResult.LIFECYCLE.IN_FLIGHT) ||
result.getLifecycle().equals(CharacterResult.LIFECYCLE.IN_FLIGHT) ||
result.getLifecycle().equals(FilmResult.LIFECYCLE.IN_FLIGHT)) {
return SearchUiModel.inProgress();
}
if (result.getLifecycle().equals(SearchResult.LIFECYCLE.LANDED_SAFELY) ||
result.getLifecycle().equals(CharacterResult.LIFECYCLE.LANDED_SAFELY) ||
result.getLifecycle().equals(FilmResult.LIFECYCLE.LANDED_SAFELY)) {
return SearchUiModel.success((JsonData) result.getData());
}
if (result.getLifecycle().equals(SearchResult.LIFECYCLE.CRASHED_BURNED) ||
result.getLifecycle().equals(CharacterResult.LIFECYCLE.CRASHED_BURNED) ||
result.getLifecycle().equals(FilmResult.LIFECYCLE.CRASHED_BURNED)) {
return SearchUiModel.failure(result.getErrorMessage());
}
return null;
});
models.doOnSubscribe(COMPOSITE_DISPOSABLE::add).subscribe(model -> report(model), throwable -> error(throwable));
As soon as my activity displays I get the following logs:-
2018-10-09 14:22:33.310 D/MainActivity: report() called with: model = [UiModel{isProgress=false, message='null', isSuccess=false, data=null}]
2018-10-09 14:22:33.311 E/MainActivity: scan() state = UiModel{isProgress=false, message='null', isSuccess=false, data=null} result = SearchResult{lifecycle=IN_FLIGHT, data=null, errorMessage='null'}
2018-10-09 14:22:33.311 D/MainActivity: report() called with: model = [UiModel{isProgress=true, message='null', isSuccess=false, data=null}]
2018-10-09 14:22:33.313 E/MainActivity: scan() state = UiModel{isProgress=true, message='null', isSuccess=false, data=null} result = CharacterResult{lifecycle=IN_FLIGHT, data=null, errorMessage='null'}
2018-10-09 14:22:33.313 D/MainActivity: report() called with: model = [UiModel{isProgress=true, message='null', isSuccess=false, data=null}]
2018-10-09 14:22:33.313 E/MainActivity: scan() state = UiModel{isProgress=true, message='null', isSuccess=false, data=null} result = FilmResult{lifecycle=IN_FLIGHT, data=null, errorMessage='null'}
2018-10-09 14:22:33.313 D/MainActivity: report() called with: model = [UiModel{isProgress=true, message='null', isSuccess=false, data=null}]
Im guessing I get these IN FLIGHT results due to my .startWith() statements.
When I either click my Search button or enter any text in my EditText views I see the following logs:-
2018-10-09 14:55:19.463 E/MainActivity: scan() state = UiModel{isProgress=false, message='null', isSuccess=true, data=com.test.model.JsonData#5e0b6f1} result = FilmResult{lifecycle=LANDED_SAFELY, data=com.test.model.JsonData#8ae4d86, errorMessage='null'}
2018-10-09 14:55:19.463 D/MainActivity: report() called with: model = [UiModel{isProgress=false, message='null', isSuccess=true, data=com.test.model.JsonData#8ae4d86}]
Why do I not see "IN FLIGHT" then "LANDED SAFELY"?
I only get "LANDED SAFELY"
Is my approach to transforming between UI Event -> Action -> Result -> UI Model anywhere close to what is described by Mr J Wharton?
Where have I gone wrong?
UPDATE (II)
My mistake was to not include all my downstream Rx within the .flatmap() operation.
CLARIFICATION
Does this pattern of UI Event ---> Action ---> Result ---> UI Model still apply for cases where there is no "Backend" as such? e.g. a Home screen could present the user with a number of options (buttons) to navigate to lower level screens within the application. The UI Event would be "Button Click" the UI Model would return with the associated Activity class to employ with the startActivity() method call.
How can I amalgamate the UI input events of a login screen into a single stream of UI events where I have two EditText fields (User Name and Password) and a Login Button.
I would want the button click UI event to contain the user name and user password entered. If I was using RxBinding to process the EditTexts and the Login button click I cannot see how I can combine these three Observables into my UI event stream and have the EditTexts validated to ensure they have data entered and then pass this user entered data to my back end login API (or maybe Google Sign In for example)
(I was adding a comment but it was too long)
I cannot help with the talks and so on presented by Jake. But regarding your last question:
Does this pattern of UI Event ---> Action ---> Result ---> UI Model
still apply for cases where there is no "Backend" as such?
It does, it is just that the backend is your application state repository.
In this kind of architecture there should only be one place of truth for your application: be it a backend, a local database, a combination of both or whatever solution is appropriate for your usecase.
Having that in mind your Action streams should modify the state either by doing calls to the backend, posting changes to a database or writing elements in the sharedSetting. Similarly, changes in your state should trigger sending Results down your streams.
The specific details would depend on what you use as a source of truth for your application.
DataFlow and State
It uses mainly Paco and Jake Wharton RxState idea plus added some more stuff.
To use UiEvent → Action, Result → UiModel transformers and always act
on a single state with the help of RxJava operators (Forming a single
stream of events, then based on their types handling actions with
different transformers, then combine results again, modifying the
state and then finally render it on the UI.
or not to use transformers and make it a little bit “simpler”.
So here is the “full” view model code without using any transformers:
class SomeViewModel(private val someRepository: SomeRepository): ViewModel() {
val uiEvents: PublishSubject<UiEvent> = PublishSubject.create()
val outputState: MutableLiveData<Result<UiState>> = MutableLiveData()
init {
uiEvents.subscribe {
when (it) {
is FirstEvent -> getSomeResultsFromRepo(it.id)
is SecondEvent -> handleSecondEvent()
}
}
}
fun getSomeResultsFromRepo(id: String) {
someRepository.getResult(id)
.map { UiState(it) }
.map { Result.success(it) }
.startWith(Result.loading())
.onErrorReturn { handleError(it) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
outputState.postValue(it)
})
}
fun handleSecondEvent() {
/* do something here */
someRepository.getSomeOtherResult()
.map { UiState(it) }
.map { Result.success(it) }
.startWith(Result.loading())
.onErrorReturn { handleError(it) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
outputState.postValue(it)
})
}
private fun handleError(error: Throwable): Result<UiState> {
return if (error is RetrofitException) {
when (error.kind) {
RetrofitException.Kind.NETWORK -> Result.failure(NetworkError(error))
RetrofitException.Kind.HTTP -> Result.failure(ServerError(error))
RetrofitException.Kind.UNEXPECTED -> Result.failure(UnknownError(error))
else -> Result.failure(UnknownError(error))
}
} else {
Result.failure(UnknownError(error))
}
}
class Factory #Inject constructor(private val someRepo: SomeRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
#Suppress("UNCHECKED_CAST")
return SomeViewModel(someRepo) as T
}
}
}
As you can see here 2 streams: a uiEvents (1st stream) which gets all the input events from the UI. As long as the UI exists it will be catching those events. Based on the event types it calls some repository functions (use cases) that are returning some response and then they are updating the model (2nd stream) with one of the possible outcomes: Success, Error or Loading.
Also transform the errors in API to so called RetrofitErrors, and based on their type it can show different error messages to the User.
There is some duplication as well that can be avoided easily, but what I wanted to show here is that it always start with a Loading result, then either a Success or an Error.
One of the most important thing is that this way "To keep state in the stream, which is a LiveData."
One benefit of this setup (just like using a BehaviourSubject) is that it will always return the last state — on orientation change it is very useful as it just loads the last available state.
Also it is highly testable as each piece can be tested in separation with providing mocked repo or view and it is also very easy to debug as we always have a current state in the stream.
Related
Force Paging Library to make Api Request
I have successfully implemented a PagedList.BoundaryCallback, which loads a list of upcoming movies from "themoviedb" database, and saves the response into the database. But it does not work the way I want it. Since the request return a list of upcoming movies, the response changes frequently. But if I already have data in my database, the onZeroItemsLoaded() method is not called. My question is, how can I force the data source, or this boundary callback to always make an api request, and refresh the content of my database from the network? public class UpcomingMoviesBoundaryCallback extends PagedList.BoundaryCallback<MovieListItemEntity> { public static final String TAG = UpcomingMoviesBoundaryCallback.class.getSimpleName(); private UpcomingMoviesRepository upcomingMoviesRepository; private int page = 1; public UpcomingMoviesBoundaryCallback(UpcomingMoviesRepository upcomingMoviesRepository) { this.upcomingMoviesRepository = upcomingMoviesRepository; } #Override public void onZeroItemsLoaded() { super.onZeroItemsLoaded(); Log.d(TAG, "onZeroItemsLoaded: "); load(); } #Override public void onItemAtEndLoaded(#NonNull MovieListItemEntity itemAtEnd) { super.onItemAtEndLoaded(itemAtEnd); Log.d(TAG, "onItemAtEndLoaded: "); load(); } #SuppressLint("CheckResult") private void load() { upcomingMoviesRepository.getUpcoming(page) .doOnSuccess(result -> { upcomingMoviesRepository.saveUpcomingMovies(result); page = result.getPage() + 1; }) .subscribeOn(Schedulers.io()) .subscribe(result -> { Log.d(TAG, "load: " + result); }, error -> { Log.d(TAG, "load: error", error); }); } } public class UpcomingMoviesRepositoryImpl implements UpcomingMoviesRepository { private static final String TAG = UpcomingMoviesRepository.class.getSimpleName(); private MovieResponseMapper movieResponseMapper = new MovieResponseMapper(); private MovieAppApi mMovieAppApi; private UpcomingDao mUpcomingDao; public UpcomingMoviesRepositoryImpl(MovieAppApi mMovieAppApi, UpcomingDao mUpcomingDao) { this.mMovieAppApi = mMovieAppApi; this.mUpcomingDao = mUpcomingDao; } #Override public Single<MovieListResponse> getUpcoming(int page) { return mMovieAppApi.upcoming(page); } #Override public Single<MovieListResponse> getUpcoming() { return mMovieAppApi.upcoming(); } #Override public void saveUpcomingMovies(MovieListResponse movieListResponse) { Executors.newSingleThreadExecutor().execute(() -> { long[] inseted = mUpcomingDao.save(movieResponseMapper.map2(movieListResponse.getResults())); Log.d(TAG, "saveUpcomingMovies: " + inseted.length); }); } #Override public LiveData<PagedList<MovieListItemEntity>> getUpcomingLiveData() { PagedList.Config config = new PagedList.Config.Builder() .setEnablePlaceholders(true) .setPageSize(12) .build(); DataSource.Factory<Integer, MovieListItemEntity> dataSource = mUpcomingDao.upcoming(); LivePagedListBuilder builder = new LivePagedListBuilder(dataSource, config) .setBoundaryCallback(new UpcomingMoviesBoundaryCallback(this)); return builder.build(); } }
Inside the repository you can query database to check if data is old then you can start an async network call that will write the result directly to the database. Because the database is being observed, the UI bound to the LiveData<PagedList> will update automatically to account for the new dataset. #Override public LiveData<PagedList<MovieListItemEntity>> getUpcomingLiveData() { if(mUpcomingDao.isDatasetValid()) //Check last update time or creation date and invalidate data if needed upcomingMoviesRepository.getUpcoming() .doOnSuccess(result -> { upcomingMoviesRepository.clearUpcomingMovies() upcomingMoviesRepository.saveUpcomingMovies(result); }) .subscribeOn(Schedulers.io()) .subscribe(result -> { Log.d(TAG, "load: " + result); }, error -> { Log.d(TAG, "load: error", error); }); }
RxJava - Wait till retryWhen finish for other observables in other Activities/Fragments
Usecase: I am developing an Android app with has a viewpager with 4 tabs, all of them are Fragments. For each tab/fragment I have to connect to a REST Api with Oauth and token expiration every 5 minutes. Current Solution: Using RxJava and retryWhen operator I can re-authenticate when receive an 401 HTTP Error. For every Observable stream subscribed and consumed and use the: retryWhen(refreshTokenAuthenticator) So when the token expires the stream consume it and then execute the real api call. Problem: this only works for one observable consumed in one subscribed but I need to allow the user to switch between tabs without blocking him/her taking into account that the 401 Error could appear in any time in any fragment in any Api Call. Question: Is there a way to make observables wait for other observables finish with onNext() which are not in the same stream/subscriber? In fact in different Fragments? So the api call scenarios will be like this: Api Call Fragment A --> request Api Call Fragment A <-- response 200 Code Api Call Fragment B --> request Api Call Fragment B <-- response 401 Code (retryWhen in action) Api Call Fragment B --> request (refreshToken) Api Call Fragment B <-- response 200 (with new access token saved in the app) Almost at the same time... Api Call Fragment C --> request Api Call Fragment C <-- response 401 Code (retryWhen in action) Observable in Fragment C Waits till Observable in Fragment B finish (onNext()) Api Call Fragment C --> request Api Call Fragment C <-- response 200 This is what I already have, each API call looks almost the same: public void getDashboardDetail() { Subscription subscription = repository.getDashboard() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .retryWhen(tokenAuthenticator) .subscribe(new RestHttpObserver<UserDataDto>() { #Override public void onUnknownError(Throwable e) { getMvpView().onError(e); } #Override public void onHostUnreachable() { getMvpView().onHostUnreachable(); } #Override public void onHttpErrorCode(int errorCode, ErrorDto errorDto) { getMvpView().onHttpErrorCode(errorCode, errorDto); } #Override public void onCompleted() { //Do nothing... } #Override public void onNext(UserDataDto response) { getMvpView().onReceiveUserData(response); } }); this.compositeSubscription.add(subscription); } And my RefreshTokenAuthenticator: public class RefreshTokenAuthenticator implements Func1<Observable<? extends Throwable>, Observable<?>> { private static final int RETRY_COUNT = 1; private static final int HTTP_ERROR_CODE = 401; #Inject private UserRepository repository; #Inject private SessionManager sessionManager; #Inject private MyApplication application; #Inject private RefreshTokenAuthenticator() { } #Override public synchronized Observable<?> call(Observable<? extends Throwable> observable) { return observable .flatMap(new Func1<Throwable, Observable<?>>() { int retryCount = 0; #Override public Observable<?> call(final Throwable throwable) { retryCount++; if (retryCount <= RETRY_COUNT && throwable instanceof HttpException) { int errorCode = ((HttpException) throwable).code(); if (errorCode == HTTP_ERROR_CODE) { return repository .refreshToken(sessionManager.getAuthToken().getRefreshToken()) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .doOnNext(tokenDto -> sessionManager.saveAuthToken(tokenDto)) .doOnError(throwable1 -> { Log.e("RefreshTokenAuth", "DoOnError", throwable1); application.logout(); }); } } // No more retries. Pass the original Retrofit error through. return Observable.error(throwable); } }); } }
1) Make source of auth tokens cache last successful result + provide method to invalidate this cached result: class Auth { private Observable<AuthToken> validToken; synchronized void invalidateAuthToken() { validToken = null; } synchronized Observable<AuthToken> getAuthToken() { if (validToken == null) { validToken = repository .refreshToken(...) // start async request .doOnError(e -> invalidateAuthToken()) .replay(1); // cache result } return validToken; // share among all subscribers } } 2) To access web service use the following pattern: Observable<Data1> dataSource1 = Observable.defer(auth.getAuthToken()) // always start from token .flatMap(token -> repository.fetchData1(token, ...)) // use token to call web service .doOnError(e -> auth.invalidateAuthToken()) .retry(N); // retry N times
Finally make it work just adding a global (in my Application class) boolean if the app is currently re-authenticating or not. It actually allows two 401 HTTP errors but the second one continues in the onNext() and re execute the initial observable. I would like to do something more reactive but at least this solves my main problem. public class RefreshTokenAuthenticator implements Func1<Observable<? extends Throwable>, Observable<?>> { private static final int RETRY_COUNT = 1; private static final int HTTP_ERROR_CODE = 401; #Inject private UserRepository repository; #Inject private SessionManager sessionManager; #Inject private MyApplication application; #Inject private RefreshTokenAuthenticator() { } #Override public Observable<?> call(Observable<? extends Throwable> observable) { return observable .flatMap(new Func1<Throwable, Observable<?>>() { int retryCount = 0; #Override public Observable<?> call(final Throwable throwable) { retryCount++; if (retryCount <= RETRY_COUNT && throwable instanceof HttpException) { int errorCode = ((HttpException) throwable).code(); if (errorCode == HTTP_ERROR_CODE) { Log.i("RefreshTokenAuth", "APPLICATION IS AUTHENTICATING = " + application.isAuthenticating); if (!application.isAuthenticating) { application.isAuthenticating = true; String refreshToken = sessionManager.getAuthToken().getRefreshToken(); return repository .refreshToken(refreshToken) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .doOnCompleted(() -> application.isAuthenticating = false) .doOnNext(tokenDto -> sessionManager.saveAuthToken(tokenDto)) .doOnError(throwable1 -> { Log.e("RefreshTokenAuth", "DoOnError", throwable1); application.logout(); }); } else { return Observable.just(1).doOnNext(o -> Log.i("RefreshTokenAuth", "Let's try another shot!")); } } } // No more retries. Pass the original Retrofit error through. return Observable.error(throwable); } }); } }
Load more on retrofit and rxJava
I'm trying load posts from blog. I use mosby + retrofit + rxjava. public class PostRepository implements IPostRepository { private Api api; private long last_id = 0; private Single<List<Post>> postList; public PostRepository(Api api) { this.api = api; } #Override public Single<List<Post>> getList() { this.load(); return postList; } private void load() { Single<List<Post>> tmp; Log.d(Configuration.DEBUG_TAG, "Loading " + last_id); tmp = api.getPostList(last_id) .map(posts -> { ArrayList<Post> postList = new ArrayList<>(); for (PostResponse post : posts) { if (last_id == 0 || last_id > post.id) { last_id = post.id; } postList.add(new Post( post.id, post.thumb, post.created_at, post.title )); } return postList; }); if (postList == null) { postList = tmp; } else { postList.mergeWith(tmp); } } #Override public Single<Post> getDetail(long id) { return api.getPost(id) .map(postResponse -> new Post( postResponse.id, postResponse.thumb, postResponse.created_at, postResponse.title, postResponse.body )); } } and api public interface Api { #GET("posts") Single<PostListResponse> getPostList(#Query("last_id") long last_id); #GET("post/{id}") Single<PostResponse> getPost(#Path("id") long id); } First query to website is ok. https://site/posts?last_id=0 But second run function getList does not work. I always get the same get query with last_id = 0, but line in console write D/App: Loading 1416 D/App: 1416 D/OkHttp: --> GET https://site/posts?last_id=0 http/1.1 if i write tmp = api.getPostList(1000) then i get true query string https://site/posts?last_id=1000 Update I rewrite code repository. public class PostRepository implements IPostRepository { private Api api; private long last_id = 0; private List<Post> postList = new ArrayList<>(); private Observable<List<Post>> o; public PostRepository(Api api) { this.api = api; } #Override public Single<List<Post>> getList() { return load(); } private Single<List<Post>> load() { return api.getPostList(last_id) .map(posts -> { for (PostResponse post : posts) { if (last_id == 0 || last_id > post.id) { last_id = post.id; } postList.add(new Post( post.id, post.thumb, post.created_at, post.title )); } return postList; }); } #Override public Single<Post> getDetail(long id) { return api.getPost(id) .map(postResponse -> new Post( postResponse.id, postResponse.thumb, postResponse.created_at, postResponse.title, postResponse.body )); } } It's work
Your problem lies in this code fragment: if (postList == null) { postList = tmp; } else { postList.mergeWith(tmp); // here } Operators on observables are performing immutable operations, which means that it always returns new stream which is a modified version of the previous one. That means, that when you apply mergeWith operator, the result of this is thrown away as you are not storing it anywhere. The most easy to fix this is to replace the old postList variable with the new stream. However, this is not optimal way of doing this. You should have a look on Subjects and emit new values within the old stream as your current solution will not affect previous subscribers as they have subscribed to a different stream
Connecting RxJava Observables in layers
I have 3 layers in my app. Layer1 subscribes to Observable from layer2. Layer2 subscribes to layer3 in order to emit returned data to layer1. Layer1 layer2.getData(data).subscribe(newData -> {Log.d("onNext", "returned");}, throwable -> {Log.d("onError", throwable.getMessage());}); Suppose layer3 has a method called downloadDataFromApi(data); public Observable<Data> getData(String data) { return Observable.create(new Observable.OnSubscribe<Data>() { #Override public void call(Subscriber<? super Data> subscriber) { Data data = new Data(); subscriber.onNext(data); subscriber.onCompleted(); // Can't find a way to connect to layer3. } }); } What do I need to do in layer2's getData() method? I basically want to have logics before returning Observable back to layer1. Does that make sense?
Just return the Observable directly. Then layer1 handles subscription as usual. class Layer2 { public Observable<Data> getData(String data) { return layer3.getData(data); } }
From what I see you have 3 layers (presentation, business logic, data access). So what you could do is the following: class PresentationLayer { private BusinessLogicLayer layer; PresentationLayer() { layer = new BusinessLogicLayer(); } public void showName() { layer.getNameWithoutRxPrefix() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<String>() { #Override public void accept(String name) throws Exception { // show name somewhere Log.d("PresentationLayer", "name: " + name); } }); } } class BusinessLogicLayer { private DataAccessLayer layer; BusinessLogicLayer() { layer = new DataAccessLayer(); } public Observable<String> getNameWithoutRxPrefix() { return layer.getName() .map(new Function<String, String>() { #Override public String apply(String name) throws Exception { return name.replace("Rx", ""); } }); } } class DataAccessLayer { public Observable<String> getName() { return Observable.just("RxAndroid"); } } As you can see, I return an Observable in my data access layer (getName), and chain another method to it in my business logic method (map) before returning it to the presentation layer.
Realm change in change listener
I try to replicate database trigger function with Realm with Rx. Once I get RealmList emitted, I do some stuff with it and save. Sadly, this results into Realm's change listener to be executed again, emitting the list over and over again. Dummy example: realm.where(MyRealmObject.class) .equalTo("state", "new") .findAll() .asObservable() .flatMap(new Func1<RealmResults<MyRealmObject>, Observable<MyRealmObject>>() { #Override public Observable<MyRealmObject> call(RealmResults<MyRealmObject> list) { return Observable.from(list); } }) .subscribe(new Action1<MyRealmObject>() { #Override public void call(final MyRealmObject object) { realm.executeTransaction(new Realm.Transaction() { #Override public void execute(Realm realm) { // do any realm change } }); } }); Once I commit the transaction in subscriber, new RealmList is emited from observable. I know why this happens, I just don't see any way how to workaround this. This takes us to my question. Is there any way how to replicate trigger functionality with realm where I will do any realm change?
Workaround can be built with helper stream determing whether next item from db should be consumed. Every data store into db should be accompanied with write into helper stream. Running test below yields: upstream: IgnoreAction{action='start', ignoreNext=false} result: 1 result: 2 result: 3 upstream: IgnoreAction{action='1', ignoreNext=true} upstream: IgnoreAction{action='2', ignoreNext=true} upstream: IgnoreAction{action='3', ignoreNext=true} So, first data ("start") is consumed, and writes triggered in onNext are ignored. #Test public void rxIgnore() throws Exception { MockDb mockDb = new MockDb(); BehaviorSubject<Boolean> ignoreNextStream = BehaviorSubject.create(false); Observable<String> dataStream = mockDb.dataSource(); dataStream.zipWith(ignoreNextStream, Data::new) .doOnNext(action -> System.out.println("upstream: " + action)) .filter(Data::isTakeNext) .flatMap(__ -> Observable.just(1, 2, 3)) .subscribe(new Observer<Integer>() { #Override public void onCompleted() { } #Override public void onError(Throwable e) { } #Override public void onNext(Integer val) { System.out.println("result: " + val); ignoreNextStream.onNext(true); mockDb.data(String.valueOf(val)); } }); mockDb.data("start"); Observable.empty().delay(1, TimeUnit.MINUTES).toBlocking().subscribe(); } private static class Data { private final String action; private final boolean ignoreNext; public Data(String action, boolean ignoreNext) { this.action = action; this.ignoreNext = ignoreNext; } public boolean isTakeNext() { return !ignoreNext; } #Override public String toString() { return "IgnoreAction{" + "action='" + action + '\'' + ", ignoreNext=" + ignoreNext + '}'; } } private static class MockDb { private final Subject<String, String> subj = PublishSubject.<String>create() .toSerialized(); public void data(String action) { subj.onNext(action); } Observable<String> dataSource() { return subj; } }