Skip to content

Instantly share code, notes, and snippets.

@alouanemed
Last active October 12, 2018 23:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alouanemed/6b5f4d5d3623ae69331de25c12dd5b98 to your computer and use it in GitHub Desktop.
Save alouanemed/6b5f4d5d3623ae69331de25c12dd5b98 to your computer and use it in GitHub Desktop.
takeUntil implementation
@GET(ApiConstants.GET_QUESTIONS_URL) Observable<RequestResponse> getQuestions();
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")));
}
Public class RequestResponse {
@SerializedName("questions")
ArrayList<Question> questions;
public ArrayList<Question> getQuestions() {
return questions;
}
}
@kaushikgopal
Copy link

  1. what exactly is the problem you're facing?
  2. you should probably return the observable directly and use that in the interactor vs having a callback

@alouanemed
Copy link
Author

alouanemed commented Dec 2, 2016

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