I need to do:
Request 2 lists of news from different websites
Combine results from requests
Sort items by date
Get 10 newest news
Save them
Show complete message
For example, I have this two observables:
Observable<RegionalNews> regionalNews;
Observable<NationalNews> nationalNews;
public interface NewsNationalService {
#GET("news/national")
Observable<News> getNationalNews();
}
public interface NewsRegionalService {
#GET("news/regional")
Observable<News> getRegionalNews();
}
You can use zip operator to call 2 requests async and save or process their data on response.
For example.
Below are two Observable
Observable<ResponseOne> responseOneObservable = getRetrofitClient().getDataOne()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
Observable<ResponseTwo> responseTwoObservable = getRetrofitClient().getDataTwo()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
Using zip operator on above two Observable as below.
Observable<ArrayList<TestData>> testDataObservable = Observable.zip(responseOneObservable, responseTwoObservable, new Func2<ResponseOne, ResponseTwo, ArrayList<TestData>>() {
#Override
public ArrayList<TestData> call(ResponseOne responseOne, ResponseTwo responseTwo) {
ArrayList<TestData> testDataList = new ArrayList();
// process data from response responseOne & responseTwo
return testDataList;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<ArrayList<TestData>>() {
#Override
public void onNext(ArrayList<TestData> testDataList) {
}
#Override
public void onCompleted() {
Log.d(TAG, "onCompleted" );
// you can show alert here or do something when completed
}
#Override
public void onError(Throwable t) {
Log.d(TAG, "onError Throwable: " + t.toString() );
}
});
If you don't want to do something specific with the combined results, then merge() is enough:
Observable<RegionalNews> regionalNews = ...;
Observable<NationalNews> nationalNews = ...;
Observable
.merge(regionalNews, nationalNews)
.ignoreElements()
.observeOn(AndroidSchedulers.mainThread())
.doOnComplete(() -> { /* show alert */ })
.subscribe()
Well it depends, as always. Do you need to process the returned values down the chain, or just save it?
In this implementation I use Single and Completable. You subscribe to the completable and you will get notified when both Singles finished.
#Test
public void name() throws Exception {
TestScheduler testScheduler = new TestScheduler();
Single<Long> request1 = Single.timer(1000, TimeUnit.MILLISECONDS, testScheduler)
.doOnSuccess(aLong -> {
System.out.println("save to db.");
});
Single<Long> request2 = Single.timer(500, TimeUnit.MILLISECONDS, testScheduler)
.doOnSuccess(aLong -> {
System.out.println("save to db.");
});
Completable completable = Single.zip(request1, request2, (aLong, aLong2) -> aLong).toCompletable();
TestObserver<Void> test = completable.test();
testScheduler.advanceTimeBy(1010, TimeUnit.MILLISECONDS);
test.assertComplete();
}
You also can use flatMapCompletable instead of doOnSuccess
#Test
public void name() throws Exception {
TestScheduler testScheduler = new TestScheduler();
Completable request1 = Single.timer(1000, TimeUnit.MILLISECONDS, testScheduler)
.flatMapCompletable(this::saveToDb);
Completable request2 = Single.timer(500, TimeUnit.MILLISECONDS, testScheduler)
.flatMapCompletable(this::saveToDb);
// need to cheat here, becuase completeable does not provide zip
Completable completable = Single.zip(request1.toSingle(() -> 1), request1.toSingle(() -> 1), (aLong, aLong2) -> aLong)
.toCompletable();
TestObserver<Void> test = completable.test();
testScheduler.advanceTimeBy(1010, TimeUnit.MILLISECONDS);
test.assertComplete();
}
private Completable saveToDb(long value) {
return Completable.complete();
}
zip is the way to combine observables. Combining their results is just a consequence.
If you want to wait for both observables to finish (complete), the easiest way is to use zip. You just don't have to use the results of your requests in the combining function. Just use this function as a way to emit something different when both of those calls finish. When this function emits an item:
[...] do something when all requests completed (show alert for example)
For example like this (executing someOtherCall when both of those requests finish):
Observable<Integer> obs1 = ...;
Observable<Long> obs2 = ...;
Observable.zip(obs1, obs2, new Func2<Integer, Long, String>() {
#Override
public String call(Integer integer, Long aLong) {
return "something completely different";
}
}).flatMap(new Func1<String, Observable<Float>>() {
#Override
public Observable<Float> call(String s) {
return performSomeOtherCall();
}
}).subscribe(...);
Related
compositeDisposable += Observable.zip(
someObservable(),
someObservableTwo(), { t1, t2 ->
Pair(first, second)
}
).zipWith(Observable.interval(0, 10, TimeUnit.SECONDS), { t1, t2 ->
t1
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
//success
}, {
//error
})
The main objective of this code to execute every 10 seconds. Logic is not wrong but somehow I am missing something. Hope someone helps
What about following solution?
The source observable will emit an event ever 1_000ms. On each event both API endpoints will be called. When the interval source observable emits a new value, while the Observable#combineLatest is subscribed, it will be unsubscribed and called again (switchMap). If you do not want to cancel out the inner stream on each new emit, you woul use flatMap (NOTE: at max 128 concurrent streams are merged by flatMap by default)
interface Api {
Observable<Integer> call1();
Observable<String> call2();
}
static final class ApiImpl implements Api {
#Override
public Observable<Integer> call1() {
return Observable.just(42);
}
#Override
public Observable<String> call2() {
return Observable.just("42");
}
}
#Test
public void interval() {
TestScheduler testScheduler = new TestScheduler();
Api api = new ApiImpl();
TestObserver<Integer> test = Observable.interval(0, 1_000, TimeUnit.MILLISECONDS, testScheduler)
.switchMap(aLong -> Observable.combineLatest(api.call1(), api.call2(), (integer, s) -> 42))
.test();
testScheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
test.assertNotComplete().assertValueCount(1)
.assertValues(42);
testScheduler.advanceTimeBy(2_000, TimeUnit.MILLISECONDS);
test.assertNotComplete().assertValueCount(3)
.assertValues(42, 42, 42);
}
I have implemented a repository pattern in MVP using RxJava2
RemoteDataSource.java
public Observable<List<A>> getAList(){
return ApiService.
getAList()
.compose(RxUtils.applySchedulers())
.doOnSubscribe(disposable -> Timber.d(..))
.doOnError(throwable -> Timber.d(..))
.doOnComplete(() -> {
Timber.d(..);
});
}
LocalDataSource.java
public Observable<List<A>> getAList(){
return mDbHelper .....from SQLBrite..
}
public void saveAList(List<<A> a){
SQlBriteTransaction...
}
Repository.java (Update)
#Inject
public Repository(DownloadUtils downloadUtils){
this.mDownloadUtils = downloadUtils;
}
#Override
public Observable<List<A>> getAList(){
return mRemoteDataSource
.getAList()
.flatMapIterable(List<A> -> a)
.flatMap(A a ->
************************************************************
return Observable.fromIterable(a.getB())
.flatMap((Function<B, ObservableSource<B>>) b ->
Observable.create(emitter ->
emitter.onNext(new
DownloadUtils().downloadFiles(b,totalListCount,emitter))))
.toList()
.toObservable()
***************************************************
.toList()
.toObservable()
.doOnNext( List<A> a -> {
--------------Only the first change in B value is inserted in Db-
mLocalDataSource.saveAList(a);
});
}
DownLoadUtils.java (Update)
void downloadBFiles(B b, int totalCount,ObservableEmitter<B> emitter){
fileCount = b.size;
b.get(index).setDataToChange(dataToChange);
*** I am using PR Downloader for aynchronous download using
RECURSION **
PRDownloader.download(remoteUrl, filePath, fileName)
.build()
.setOnStartOrResumeListener(() -> {
})
.setOnProgressListener(progress -> {
int progressPercent = (int) (progress.currentBytes *
100 / progress.totalBytes);,
})
.start(new OnDownloadListener() {
#Override
public void onDownloadComplete() {
********************* emitter.onComplete() ******************
#Override
public void onError(Error error) {
}
}
Presenter.java
void getVideosFromRepo(){
disposable = mRepository
.getAList()
.doOnSubscribe(d _-> "Started Loading")
.subscribe(
//OnNext
------------- Here the OnNext is being called before Asynchronous Operation completes!!-------
List<A> a -> mView.setAList(a);
)
}
Above presenter implementation, returns the List in the onNext of the Presenter even before asynchronous download completes... what changes are needed , so that onNext(subscribe) is called after all the download Has Completed.!!!
You are using asynchronous services outside of the RxJava observer chain, so RxJava has no way to manage the data being passed. Since downloadBFiles() uses a separate observer chain, you have lost the thread, so to speak.
Rather than using doOnNext() to trigger the download, you will need to use flatMap() so that the download result is included in your observer chain.
I've got an EditText view and TextWatcher for it, in onTextChanged method I have to requst server for result with query from EditText field.
In my presenter I use rx for that, but i need to delay search until user's input ends. At this moment i've got this:
service.getData(query)
.delaySubscription(REQUEST_DELAY_FROM_SERVER, TimeUnit.MILLISECONDS, Schedulers.io())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
data-> {
getViewState().showData(data);
},
error -> {
Log.e(this.getClass().getSimpleName(), error.getMessage(), error);
}
);
But delaySubscription does not work as desired. It collects all call and after delay sends every of them. I have to do same as if I had used handler.postDelayed(), when only once request will be send.
Edit 2:
The saple of a presenter in RxJava2
class Presenter {
private PublishSubject<String> queryPublishSubject = PublishSubject.create();
public Presenter() {
queryPublishSubject
.debounce(1000, TimeUnit.MILLISECONDS)
// You might want to skip empty strings
.filter(new Predicate<CharSequence>() {
#Override
public boolean test(CharSequence charSequence) {
return charSequence.length() > 0;
}
})
// Switch to IO thread for network call and flatMap text input to API request
.observeOn(Schedulers.io())
.flatMap(new Function<CharSequence, Observable<...>() {
#Override
public Observable<...> apply(final CharSequence charSequence) {
return ...; // Call API
}
})
// Receive and process response on Main thread (if you need to update UI)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...);
}
public void onSearchTextChanged(String query) {
queryPublishSubject.onNext(query);
}
}
Edit 1:
The same code in RxJava 1:
class Presenter {
private PublishSubject<String> queryPublishSubject = PublishSubject.crate();
public Presenter() {
queryPublishSubject
.debounce(1000, TimeUnit.MILLISECONDS)
// You might want to skip empty strings
.filter(new Func1<CharSequence, Boolean>() {
#Override
public Boolean call(CharSequence charSequence) {
return charSequence.length() > 0;
}
})
// Switch to IO thread for network call and flatMap text input to API request
.observeOn(Schedulers.io())
.flatMap(new Func1<CharSequence, Observable<...>() {
#Override
public Observable<...> call(final CharSequence charSequence) {
return ... // Call API
}
})
// Receive and process response on Main thread (if you need to update UI)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...);
}
public void onSearchTextChanged(String query) {
queryPublishSubject.onNext(query);
}
}
Initial answer (with RxBinding and RxJava 1)
The correct answer is to use Debounce, but besides that there are some other tricks you might find useful
textChangeListener = RxTextView
.textChanges(queryEditText)
// as far as I know, subscription to textChanges is allowed from Main thread only
.subscribeOn(AndroidSchedulers.mainThread())
// On subscription Observable emits current text field value. You might not need that
.skip(1)
.debounce(1000, TimeUnit.MILLISECONDS)
// You might want to skip empty strings
.filter(new Func1<CharSequence, Boolean>() {
#Override
public Boolean call(CharSequence charSequence) {
return charSequence.length() > 0;
}
})
// Switch to IO thread for network call and flatMap text input to API request
.observeOn(Schedulers.io())
.flatMap(new Func1<CharSequence, Observable<...>() {
#Override
public Observable<...> call(final CharSequence charSequence) {
return ... // Call API
}
})
// Receive and process response on Main thread (if you need to update UI)
.observeOn(AndroidSchedulers.mainThread())
I have something similar for an address research combining with RxAndroid could give something like that :
RxTextView.textChanges(searchEditText)
.debounce(100, TimeUnit.MILLISECONDS)
.subscribe(....);
The debounce operator will wait in this case that the observable stop to emit for 100ms before emitting the next value.
Try using debounce instead. For eg. code below look for changes in a TextView and do something when there is a change but with a debounce of 100 ms
RxTextView
.textChanges(queryEditText)
.debounce(100, TimeUnit.MILLISECONDS)
.doOnNext(new Action1<CharSequence>() {
#Override
public void call(CharSequence charSequence) {
}
})
.subscribe();
I'm using RxSearchView to emit out the results of a search query from an API to a recyclerview. However, if one of those query fails, onError() is called(which is expected) but the subscription as a whole is also canceled. Subsequent queries are not executed at all.
How should i modify the code so that the call to onError() is prevented when a query fails and the next incoming queries are executed normally?
Here's a code snippet:
subscription = RxSearchView.queryTextChanges(searchView)
.debounce(500, MILLISECONDS)
.filter(charSequence -> !TextUtils.isEmpty(charSequence))
.map(CharSequence::toString)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.io())
.switchMap(query -> apiService.getSearchResults(query))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<SearchResponse>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(SearchResponse searchResponse) {
if (searchResponse.getStatus().equals("OK")) {
//update Adapter
} else {
//update error views
}
}
});
P.S: I am using switchMap() so that the results of old queries are ignored, if the results of new query has arrived.
You have to handle this error and return an object instead. You can do it, for example, by using onErrorResumeNext operator with apiService.getSearchResults(query) call. What you are going to return - depends on you, you can even return null if you want, but better to create some wrapper which can carry both response status flag and normal response if received.
Something like:
subscription = RxSearchView.queryTextChanges(searchView)
.debounce(500, MILLISECONDS)
.filter(charSequence -> !TextUtils.isEmpty(charSequence))
.map(CharSequence::toString)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.io())
.switchMap(query -> apiService
.getSearchResults(query)
.onErrorResumeNext(error -> null)
)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<SearchResponse>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(SearchResponse searchResponse) {
if (searchResponse != null && searchResponse.getStatus().equals("OK")) {
//update Adapter
} else {
//update error views
}
}
});
Of course, this is naive example with using null, in reality you need to write error handling logic. Better to return wrapper, because if using RxJava 2, then it doesn't support null.
current code:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService service = retrofit.create(APIService.class);
Call<ResponseWrap> call = service.getNewsData();
call.enqueue(new Callback<ResponseWrap>() {
#Override
public void onResponse(Call<ResponseWrap> call1, Response<ResponseWrap> response) {
if (response.isSuccess()) {
ResponseWrap finalRes = response.body();
for(int i=0; i<finalRes.getResponse().getResults().size(); ++i){
String title = finalRes.getResponse().getResults().get(i).getWebTitle();
News n = new News(titleCategory, title, null);
newsList.add(n);
}
AdapterRecommendation adapter = new AdapterRecommendation(getApplicationContext(), newsList);
listView.setAdapter(adapter);
}
else{
Toast.makeText(getApplicationContext(), "onResponse - something wrong" + response.message(), Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(Call<ResponseWrap> call1, Throwable t) {
Toast.makeText(getApplicationContext(), "exception: " + t.getMessage(), Toast.LENGTH_LONG).show();
}
});
works fine.
Now i want to make multiple calls (number of call will be decided at run time) and all calls gives data in same format. data from all calls needs to be add to newsList. Once data is available from all calls and added to newsList, call
AdapterRecommendation adapter = new AdapterRecommendation(getApplicationContext(), newsList);
listView.setAdapter(adapter);
Can anyone help me what is the best way to get data from multiple calls and wait until all request is not over in retrofit 2.0.
The clean and neat approach to wait until all your requests will be done is to use Retrofit2 in conjunction with RxJava2 and its zip function.
What zip does is basically constructs new observable that waits until all your retrofit Observable requests will be done and then it will emit its own result.
Here is an example Retrofit2 interface with Observables:
public interface MyBackendAPI {
#GET("users/{user}")
Observable<User> getUser(#Path("user") String user);
#GET("users/{user}/photos")
Observable<List<Photo>> listPhotos(#Path("user") String user);
#GET("users/{user}/friends")
Observable<List<User>> listFriends(#Path("user") String user);
}
In the code where you going to make multiple requests and only after all of them will complete do something else you can then write the following:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.example.com/")
.build();
MyBackendAPI backendApi = retrofit.create(MyBackendAPI.class);
List<Observable<?>> requests = new ArrayList<>();
// Make a collection of all requests you need to call at once, there can be any number of requests, not only 3. You can have 2 or 5, or 100.
requests.add(backendApi.getUser("someUserId"));
requests.add(backendApi.listPhotos("someUserId"));
requests.add(backendApi.listFriends("someUserId"));
// Zip all requests with the Function, which will receive the results.
Observable.zip(
requests,
new Function<Object[], Object>() {
#Override
public Object apply(Object[] objects) throws Exception {
// Objects[] is an array of combined results of completed requests
// do something with those results and emit new event
return new Object();
}
})
// After all requests had been performed the next observer will receive the Object, returned from Function
.subscribe(
// Will be triggered if all requests will end successfully (4xx and 5xx also are successful requests too)
new Consumer<Object>() {
#Override
public void accept(Object o) throws Exception {
//Do something on successful completion of all requests
}
},
// Will be triggered if any error during requests will happen
new Consumer<Throwable>() {
#Override
public void accept(Throwable e) throws Exception {
//Do something on error completion of requests
}
}
);
That's all :)
Just in case wanna show how the same code looks like in Kotlin.
val retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com/")
.build()
val backendApi = retrofit.create(MyBackendAPI::class.java)
val requests = ArrayList<Observable<*>>()
requests.add(backendApi.getUser())
requests.add(backendApi.listPhotos())
requests.add(backendApi.listFriends())
Observable
.zip(requests) {
// do something with those results and emit new event
Any() // <-- Here we emit just new empty Object(), but you can emit anything
}
// Will be triggered if all requests will end successfully (4xx and 5xx also are successful requests too)
.subscribe({
//Do something on successful completion of all requests
}) {
//Do something on error completion of requests
}
If you don't mind adding one more dependency you could use RxAndroid.
In particular, you should change your Service interface with something similar to this:
#GET("/data")
Observable<ResponseWrap> getNewsData();
Now, you can do this:
Observable
.range(0, **numberOfTimes**, Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(new Action1<Throwable>() {
#Override
public void call(Throwable throwable) {
Log.e("error", throwable.toString());
}
})
.concatMap(new Func1<Integer, Observable<ResponsWrapper>>() {
#Override
public Observable<ResponsWrapper> call(Integer integer) {
Log.i("news", "nr:" + integer);
//Does the call.
return service.getNewsData(integer);
}
}).concatMap(new Func1<ResponsWrapper, Observable<News>>() {
#Override
public Observable<News> call(final ResponsWrapper responsWrapper) {
return Observable.fromCallable(new Func0<News>() {
#Override
public News call() {
//change the result of the call to a news.
return new News(responsWrapper.category,responsWrapper.title,null);
}
});
}
}).toList().subscribe(new Action1<List<News>>() {
#Override
public void call(List<News> newList) {
AdapterRecommendation adapter = new AdapterRecommendation(getApplicationContext(), newsList);
listView.setAdapter(adapter);
}
});
Just change numberOfTimes and it will work! Hope it helps.
P.s. maybe there are cleaner ways to do this.
You can achieve it by making synchronous retrofit calls. To avoid NetworkOnUiException, I am doing this inside asynctask.
List<Something> list = new ArrayList();
public void doInBackground(){
for(int i = 0; i < numberOfCalls; i++){
Call<Something> call = service.method1("some_value");
List<Something> list = call1.execute().body();
list.add(list1);
}
}
public void onPostExecute(){
AdapterRecommendation adapter = new AdapterRecommendation(getApplicationContext(), newsList);
listView.setAdapter(adapter);
}
This will ensure that the second call happens only after the first one has completed.
If you are using rx-java, you can use Zip/flatMap operator as used in this answer.
for anybody checking this question. This works for me (Kotlin)
fun manyRequestsNetworkCall(requests: ArrayList<Observable<*>>, activity: Activity){
Observable.zip(requests){results ->
activity.runOnUiThread(Runnable {
//do something with those results
// runOnUiThread solves the problem cannot do something on background thread
})
// observeOn and subscribeOn solvesthe problem of NetworkOnMainThreadException
}.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.doOnSubscribe { userWorkdysResponse.value = Response.loading((requestType)) }
.subscribe ({
// do something when all the requests are done
},{
// do something if there is an error
})
}
Here is a solution based on kotlin coroutines.
//turn the request methods into suspend functions
#GET("data1")
suspend fun getData(): Response<Data1>
#GET("data2")
suspend fun getData2(): Response<Data2>
//define a data class to ecapsulate data from several results
class Data{
val data1: Data1,
val data2: Data2
}
//generic class to encapsulate any request result
sealed class Result<out T : Any?> {
data class Success<out T : Any?>(val data: T) : Result<T>()
data class Error(val message: String, val exception: Exception?) : Result<Nothing>()
}
scope.launch {
val result = withContext(Dispatchers.IO) {
try {
//start two requests in parallel
val getData1Task = async { webservice.getData1() }
val getData2Task = async { webservice.getData2() }
//await for both to finish
val data1Response = getData1Task.await()
val data2Response = getData2Task.await()
//process the response
if (data1Response.isSuccessful && data2Response.isSuccessful)
Result.Success(Data(data1Response.body()!!,data2Response.body()!!))
else
Result.Error("server error message", null)
} catch (e: Exception) {
Result.Error(e.message.orEmpty(), e)
}
}
//main thread
result.run {
when (this) {
is Result.Success -> {
//update UI
}
is Result.Error -> {
toast(message)
log(message)
}
}
}
}