Skip to content

Instantly share code, notes, and snippets.

@ragdroid
Last active September 11, 2016 16:23
Show Gist options
  • Save ragdroid/4f3256fd27cfbc7652a9faa94b43225f to your computer and use it in GitHub Desktop.
Save ragdroid/4f3256fd27cfbc7652a9faa94b43225f to your computer and use it in GitHub Desktop.
Session Renew Problem - DroidconIN 2016
// Use concat and repeat to renew the session repeatedly
Observable<Session> getRenewedSession() {
Observable<Session> sessionObservable =
SessionManager.getCurrentSessionObservable()
.filter(new Func1<Session, Boolean>() {
@Override
public Boolean call(Session session) {
return session.isValid();
}
});
Observable<Session> renewSession =
SessionManager.getCurrentSessionObservable()
.flatMap(new Func1<Session, Observable<Session>>() {
@Override
public Observable<Session> call(Session session) {
return authService.renewSession(session.getSessionId())
.repeatWhen(
new Func1<Observable<? extends Void>, Observable<?>>() {
@Override
public Observable<?> call(
Observable<? extends Void> observable) {
return observable.delay(
Session.EXPIRY_TIME_IN_MILLIS,
TimeUnit.MILLISECONDS);
}
});
}
});
return sessionObservable.concatWith(renewSession);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment