Last active
October 12, 2018 23:23
-
-
Save alouanemed/6b5f4d5d3623ae69331de25c12dd5b98 to your computer and use it in GitHub Desktop.
takeUntil implementation
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
@GET(ApiConstants.GET_QUESTIONS_URL) Observable<RequestResponse> getQuestions(); |
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
public void performGetElQuestions(String query, QuestionsRequestServerCallback callback) { | |
getFreshNetworkData()// | |
.publish(network ->// | |
Observable.merge(network,// | |
getCachedDiskData().takeUntil(network))) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new DisposableObserver<Question>() { | |
@Override | |
public void onComplete() { | |
callback.onQuestionsReady(mQuestionsList); | |
} | |
@Override | |
public void onError(Throwable e) { | |
callback.onQuestionsFailed(); | |
} | |
@Override | |
public void onNext(Question question) { | |
// mQuestionsList is an arraylist | |
mQuestionsList.add(question); | |
} | |
}); | |
private Observable<Question> getCachedDiskData() { | |
List<Question> list = new ArrayList<>(); | |
//get cached data from SQLite or disk | |
return Observable.fromIterable(list)// | |
.doOnSubscribe((data) -> new Handler(Looper.getMainLooper())// | |
.post(() -> Timber.d("(disk) cache subscribed")))// | |
.doOnComplete(() -> new Handler(Looper.getMainLooper())// | |
.post(() -> Timber.d("(disk) cache completed"))); | |
} | |
private Observable<Question> getFreshNetworkData() { | |
return apiService.getQuestions() | |
.flatMap(Observable::fromIterable) | |
.doOnSubscribe((data) -> new Handler(Looper.getMainLooper())// | |
.post(() -> adapterSubscriptionInfo.add("(network) subscribed")))// | |
.doOnComplete(() -> new Handler(Looper.getMainLooper())// | |
.post(() -> adapterSubscriptionInfo.add("(network) completed"))); | |
} |
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
Public class RequestResponse { | |
@SerializedName("questions") | |
ArrayList<Question> questions; | |
public ArrayList<Question> getQuestions() { | |
return questions; | |
} | |
} |
Hey kaushik, I am using MVP in my app so I need the callback to connect with the presenter .
As you can see the freshnetworkethod returns a < question > while the API response is RequestResponse so how to connect both ?
Thanks again
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
callback