Created
March 24, 2017 13:10
-
-
Save MaksTuev/364443dc6bf705b4063f770137a6e8e7 to your computer and use it in GitHub Desktop.
hybrid rx request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Осуществляет гибридный запрос, в методе происходит объединение данных приходящих с сервера и из кеша | |
* | |
* @param cacheRequest запрос к кешу | |
* @param networkRequestCreator функция, которая должна вернуть запрос к серверу, | |
* Integer параметр этой функции определяет {@link QueryMode} | |
* @param <T> тип возвращаемого значения | |
*/ | |
protected <T> Observable<T> hybridQuery(Observable<T> cacheRequest, | |
Func1<Integer, Observable<T>> networkRequestCreator) { | |
return cacheRequest | |
.onErrorResumeNext(e -> { | |
Logger.e(e, "Error when getting data from cache"); | |
return networkRequestCreator.call(QUERY_MODE_FORCE); | |
}) | |
.flatMap(cache -> { | |
boolean cacheExist = cache != null; | |
@QueryMode int queryMode = cacheExist | |
? QUERY_MODE_ONLY_IF_CHANGED | |
: QUERY_MODE_FORCE; | |
return Observable.concat( | |
cacheExist ? Observable.just(cache) : Observable.empty(), | |
networkRequestCreator.call(queryMode)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment