I am using RxJava on Android to perform a login operation.
I need to pass in a username, password and a boolean flag. The username and password and sent to a server for verification and once a response is returned I need to use the flag to determine what to do next.
Since the login operation is asynchronous, I want to ensure that when the response returns that I will still have access to the username, password and flag that I passed in at the beginning.
Here is the initial way I coded this up that I believe has problems:
public Observable<Result> execute1(final String username, final String password, final boolean shouldSaveUsername) {
return mLoginNetwork
.loginWithCredentials(username, password)
.map(new Func1<Response<Void>, LoginObject>() {
#Override
public LoginObject call(Response<Void> response) {
if (!response.isSuccessful()) {
Exceptions.propagate(new HttpException(response));
}
return new LoginObject(username, password, shouldSaveUsername);
}
})
.doOnNext(new Action1<LoginObject>() {
#Override
public void call(LoginObject loginObject) {
if (loginObject.shouldSaveUsername) {
saveUsername(username);
}
}
})
.flatMap(new Func1<Entitlement, Observable<Result>>() {
#Override
public Observable<Result> call(LoginObject loginObject) {
return mNetwork
.fetchSomething();
}
});
}
When I call execute1() it returns an Observable which I cache and then subscribe to. If an Android configuration change occurs I unsubscribe from the Observable but keep it in a cache. Once the configuration change is complete I take the Observable out of the cache and resubscribe to it. When I resubscribe the loginWithCredentials call would need to be made again, but when it returns the username, password and boolean flag would no longer exist and therefore I wouldn't be able to use them in my chain which is a problem.
So, how to solve this issue?
I need a way for the input data to the Observable to become part of the Observable so that when I cache the Observable the input data is also cached.
Here is a proposed solution below:
public Observable<Result> execute2(String username, String password, boolean shouldSaveUsername) {
return Observable
.just(new LoginData(username, password, shouldSaveUsername))
.flatMap(new Func1<LoginData, Observable<LoginData>>() {
#Override
public Observable<?> call(final LoginData loginData) {
return mLoginNetwork
.loginWithCredentials(loginData.getUsername(), loginData.getPassword())
.map(new Func1<Response<Void>, LoginData>() {
#Override
public LoginData call(Response<Void> response) {
if (!response.isSuccessful()) {
Exceptions.propagate(new HttpException(response));
}
return loginData;
}
});
}
})
.doOnNext(new Action1<LoginData>() {
#Override
public void call(LoginData loginData) {
if (loginData.shouldSaveUsername) {
saveUsername(username);
}
}
})
.flatMap(new Func1<LoginData, Observable<Result>>() {
#Override
public Observable<Result> call(LoginData loginData) {
return mNetwork
.fetchSomething();
}
});
}
What I'm attempting to do is to make the input data part of the stream right away by using Observable.just() to take the input data and make it into an Observable and then let the rest of the downstream operations receive it as an input. I assume that if I now cache the observable and resubscribe later that the input data is now embedded in my observable and can be accessed in any of the operators later.
Have I solved my problem in my proposed solution in a "normal" RxJava / functional way? Are there better ways to approach this problem?
The username/password/save-flag are passed in to execute1() as parameters, marked as final. Then, in your anonymous nested classes, you make explicit references to those values, "closing over" them. The resulting observable chain has everything bound to it that it needs in order to operate.
Subscribing to the observable again will use the original username/password/save-flag.
Related
I want to implement a logic using RxJava in my android application, which requires three parallel api calls. Only the third api call has a retry logic. If, after having three attempts, the success is achieved then a subsequent call will be made for the fourth api, else only the result of first and second api calls will be passed on to the subscriber.
I tried to achieve this using Zip operator but then got stuck with retry logic for third api call.
Observable<String> observable1 = Observable.just("A","B");
Observable<Integer> observable2 = Observable.just(1,2);
Observable<Boolean> observable3 = Observable.just(Boolean.TRUE, Boolean.FALSE);
Observable.zip(observable1, observable2, observable3, new Function3() {
#Override
public Object apply(String s, Integer integer, Boolean aBoolean) throws Exception {
if (aBoolean==null){
alphabets3.retry(3).doOnComplete(new Action() {
#Override
public void run() throws Exception {
// the result will never be used
}
});
}
return s+integer+aBoolean;
}
}).subscribe(new Observer<Object>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(Object o) {
Log.e("onNext-->", o.toString());
}
#Override
public void onError(Throwable e) {
}
#Override
public void onComplete() {
}
});
if any Observable failed in the Zip operator, Zip will fail the stream, the only way I know to achieve parallel execution and error handling with Zip, is to add onErrorResumeNext to each Observable, that map the error to a new model to deal with later .. and handling what you want to do in the zip mapping function ... for example
Obsevable.zip(
observable1.onErrorResumeNext{Observable.just(Model(it)},
observable2.onErrorResumeNext{Observable.just(Model(it)},
observable3.retryWhen {t is TimeOutException} //here you can add your retry logic
.onErrorResumeNext(t -> Observable.just(Model(t)),(m1 , m2, m3) -> Result())
I'd like to see an Android java example of how to sequence a chain of async (= nonblocking) RESTful Volley requests.
Is this what RxAndroid is used for?
If so, I'd like to see the example using RxAndroid.
If not, I'd still like to see a good example w/out diving into CALLBACK HELL!
I tried to do so but ended up in CBHell:
Need to send multiple Volley Requests - in a sequence
I want my result from my 1st request to be used in the 2nd request. Then the result from the 2nd request I want used in the 3rd request. Please, how do I chain such Volley requests?
You could use Rx to chain multiple requests by using the flatMap method.
flatMap requires you to return another Observable of the type of your chosing thus allowing you do something async with another type.
All of the examples below are made with the new rx v2. But all methods and mechanics also apply to v1
Example:
final MyVolleyApi api = new MyVolleyApi();
api.getName()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(new Function<String, ObservableSource<Integer>>() {
#Override
public ObservableSource<Integer> apply(String name) throws Exception {
return api.getAgeForName(name);
}
})
.flatMap(new Function<Integer, ObservableSource<Date>>() {
#Override
public ObservableSource<Date> apply(Integer age) throws Exception {
return api.getYearOfBirthForAge(age);
}
})
.doOnError(new Consumer<Throwable>() {
#Override
public void accept(Throwable throwable) throws Exception {
// handle the exception that occurred during one of the api calls
}
})
.subscribe(new Consumer<Date>() {
#Override
public void accept(Date date) throws Exception {
// do something with the 3rd argument here
}
});
This is the MyVolleyApi dummy class:
public class MyVolleyApi {
public Observable<String> getName() {
return Observable.just("Rx");
}
public Observable<Integer> getAgeForName(String name) {
return Observable.just(24);
}
public Observable<Date> getYearOfBirthForAge(int age) {
return Observable.just(new Date());
}
}
This could apply to anything, it's not volely specific at all
My goal
I want to check if the server's token is still valid, let's say I know that information just by calling this getter : preferenceHelper.isTokenValid(). Then, if the token is invalid, calling a request to get a new token and updating the token locally, THEN, proceed with the next request to post the point to the server. That's because I need a valid token in order to make any further server request.
Let say I have those two server request that returns Observable:
This request is meant to get the server token, then upon reception, updating it.
Observable<Response<EntityToken>> updateServerToken = retrofitApi.authenticate(username,password);
This request is meant to post the current location to the server, then if it succeed, return the saved point
Observable<Response<EntityPoint>> updateServerToken = retrofitApi.postPoint(point);
Issues i'm facing currently:
Both observable that needs to be merged are from different type
Executing the token update request only if it needs to
Waiting for the token update request to complete before executing the request to post points
How should I write my RxJava Observable to satisfy all those condition?
First, I would create a method that checks if the entityToken is valid or not. If valid, use Observable.just() but you have to create an instance of Response somehow. If invalid, then call the server using the API in your requirement retrofitApi.authenticate(). Either path is taken, the method getTokenObservable() emits Observable<Response<EntityToken>>.
public Observable<Response<EntityToken>> getTokenObservable(EntityToken entityToken, String username, String password) {
boolean isTokenValid = preferenceHelper.isTokenValid(entityToken);
if (isTokenValid) {
//my assumption that you have something like this
Response<EntityToken> responseToken = new Response<EntityToken>();
responseToken.setEntityToken(entityToken);
return Observable.just(new Response<EntityToken>(entityToken.class));
} else {
Observable<Response<EntityToken>> updateServerToken = retrofitApi.authenticate(username, password);
return updateServerToken;
}
}
and then when calling it, use flatMap() which take emisssions of Observable<Response<EntityToken>> and returns emissions of Observable<Response<EntityPoint>>. Subscribe and proceed as normal.
Observable<Response<EntityToken>> updatePointObservable = getTokenObservable(entityToken, username, password);
updatePointObservable
.flatMap(new Func1<Response<EntityToken>, Observable<Response<EntityPoint>>>() {
#Override
public Observable<Response<EntityPoint>> call(Response<EntityToken> responseToken) {
EntityToken entityToken = responseToken.getEntityToken(); //my assumption
saveTokenLocally(entityToken); //this is where you save your token locally, change to the right method that you have
Observable<Response<EntityPoint>> updateServerTokenObservable = retrofitApi.postPoint(point, entityToken); //pass your entityToken to the call?
return updateServerTokenObservable;
}
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Response<EntityPoint>>() {
#Override
public void onCompleted() {
//your own logic
}
#Override
public void onError(Throwable e) {
//your own logic
}
#Override
public void onNext(Response<EntityPoint> entityPoint) {
//your own logic
}
});
As there is a dependency between the three calls, merge does not make any sense. instead, use flatMap:
Observable<Response<EntityPoint>> response =
retrofitApi.isTokenValid()
.flatMap(isValid ->
isValid
? Observable.just("")
: retrofitApi.authenticate(username,password)
.doOnNext(token -> doSomethingWithTheToken(token)
)
.flatMap(dummy -> retrofitApi.postPoint(point));
I realize similar questions have been asked but I am new to android and find the answers a bit confusing since they are in a slightly different context.
I have looked at CountDownLatch aswell as using Threads and am not sure which method to use. Any help would be much appreciated. I have also tried using apply() instead of commit() for SharedPreferences.
I am making 2 retrofit2 calls from LoginActivity. I need the token from the first call to use in the second call. I am saving the token to a string in sharedpreferences in the onResponse method of the first retrofit call.
In my second call the value of serverToken is coming back as the token set in previous run of the app
1st call(getToken) onResponse
call.enqueue(new retrofit2.Callback<TokenResponse>() {
#Override
public void onResponse(Call<TokenResponse> call, retrofit2.Response<TokenResponse> response) {
if (response.isSuccessful()) {
TokenResponse tokenResponse = response.body();
LoginActivity.editor.putString("serverToken", tokenResponse.getAccessToken());
LoginActivity.editor.commit();
} else {
Log.i("Server Token", "failed");
}
}
}
LoginActivity
public class LoginActivity extends AppCompatActivity {
public static SharedPreferences preferences;
public static SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
authenticationController = new AuthenticationController();
preferences = PreferenceManager.getDefaultSharedPreferences(this);
editor = preferences.edit();
}
public void onLoginClicked(View view) {
getToken(); //FIRST RETROFIT CALL
connectToPush(); //SECOND CALL WHERE I NEED TOKEN FROM FIRST CALL
}
public void getToken() {
authenticationController.login(grantType, username, password);
}
public void connectToPush() {
authenticationController.connectToPush();
}
My Second Retrofit call
public void connectToPush(){
Log.i("sharedpreferencesToken", LoginActivity.preferences.getString("serverToken", "null serverToken"));
}
The onResponse() method is a callback interface, which ,simply putting it, means that is where you get the info back from your request\event (you called, it came back, hence callback) and implement what you want to do with it (it's an interface, you implement it, hence the #Override annotation).
this means:
You don't need CountDownLatch, at least not in this case, and Retrofit2 takes care of the threading for you.
no real need for SharedPreferences, you can just call the method you want straight from that callback, since the info is in that instance (unless you want to save it for reasons other than the next request, see next...).
if you want to locally store the value because you need it later (or to use as an auto-login thing later, you can use SharedPreferences, but you don't need to get your value from there in that instance - since it exists in the callback instance (your saving the value right there, it's redundant to load it from the Prefs again while the response holds the exact value that can be simply passed.
so:
call.enqueue(new retrofit2.Callback<TokenResponse>() {
#Override
public void onResponse(Call<TokenResponse> call, retrofit2.Response<TokenResponse> response) {
if (response.isSuccessful()) {
TokenResponse tokenResponse = response.body();
//right here you can call the other request and just give it the token
connectToPush(tokenResponse);
//if you really need to, save your value
LoginActivity.editor.putString("serverToken", tokenResponse.getAccessToken());
LoginActivity.editor.commit();
} else {
Log.i("Server Token", "failed");
}
}
}
And in your second call:
public void connectToPush(TokenResponse tokenFromFirstRequest){
//fire the next request using your token as a param!
}
Well i ended up finding the solution. Found the answer on the retrofit github
"Use the callback from method 1 to trigger method 2"
I moved connectToPush() to the onResponse of the first call.
call.enqueue(new retrofit2.Callback<TokenResponse>() {
#Override
public void onResponse(Call<TokenResponse> call, retrofit2.Response<TokenResponse> response) {
if (response.isSuccessful()) {
TokenResponse tokenResponse = response.body();
LoginActivity.editor.putString("serverToken", tokenResponse.getAccessToken());
LoginActivity.editor.commit();
connectToPush(); //MOVED TO HERE
} else {
Log.i("Server Token", "failed");
}
}
}
Feel free to delete my question. I will leave it as it may help someone else
You can call connectToPush(); from the onResponse part of your Retrofit call.
I've been wondering what's the best way to tackle the issue of token refresh.
I'm connecting to an API which supplies me with a auth-token, if sometime time during the calls i get a INVALID_AUTH i need to re-authenticate.
So for the naive implementation i did this
#SuppressWarnings("unchecked")
#Override
public Observable<User> getUsers() {
return runCommandAndrefreshAuthIfNecessary(new RequestCommand() {
#Override
public Observable create() {
return createService(UsersApi.class).getUsers();
}
});
}
private Observable runCommandAndrefreshAuthIfNecessary(final RequestCommand command) {
return command.create()
.onErrorResumeNext(new Func1<Throwable, Observable<?>>() {
#Override
public Observable<?> call(Throwable throwable) {
return handleRefreshToken(command);
}
});
}
private Observable<?> handleRefreshToken(final RequestCommand command) {
return refreshToken().flatMap(new Func1<Boolean, Observable<?>>() {
#Override
public Observable<?> call(Boolean aBoolean) {
return command.create();
}
});
}
As you can see i'm just wrapping the retrofit command, if i get an error i run refreshToken(), the token refreshes and i run the retrofit command again, so finally the Observable is passed back to the subscriber. Works as expected.
The thing i'm struggling with, is what happens i a multiple calls are made, for example, i'm calling getUsers and getFlags one after another. both of them get the INVALID_AUTH, currently both of the fire refreshToken(), which is bad.
i'm looking for a rx-java way to manage the calls, meaning after the first call of getUsers fires refreshToken, any call after that needs to wait for the refreshToken to end, only then fire the retrofit command.
Any suggestion will be appreciated.
You can use .cache() on the Observable for the token refreshing:
http://reactivex.io/documentation/operators/replay.html