Help in composing multiple network calls and accumulate the result in Rxjava. (I am using in an Android application.)
State
-- List<City> cityList;
City
- cityId;
RestCall 1
Observable<State> stateRequest = restService.getStates();
RestCall 2
Observable<CityDetail> cityRequest = restService.getCityDetail(cityId);
In UI i have to display list of cities after getting all the details of each city and then show in the listview.
How do i achieve the parllel network calls and accumulate the result. ?
I want all the city detail results to be put in List in source State 'object'. As state object has some information which need to be dislayed as well.Is this possible ?
stateRequest ???
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<State>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(State result) {
// Get city list and display
}
});
I checked this example which shows how we can zip more tha one observable response. Below snippet shows 3 observables combined.
But in my case i have to make 20 network calls parallel or sequential ( i mean in background but one after another). How do i achieve this. Any help or directions ?
https://gist.github.com/skehlet/9418379
Observable.zip(f3Observable, f4Observable, f5Observable, new Func3<String, Integer, Integer, Map<String, String>>() {
#Override
public Map<String, String> call(String s, Integer integer, Integer integer2) {
Map<String, String> map = new HashMap<String, String>();
map.put("f3", s);
map.put("f4", String.valueOf(integer));
map.put("f5", String.valueOf(integer2));
return map;
}
I think that your code can be simplified to something like this, as your use of the zip operator is close to the use of toList operator
stateRequest
.subscribe(State state -> {
Observable.from(state.getCityList())
.flatMap(City city -> restService.getCityDetail(city.getId())
.toList()
.subscribe(List<City> cities -> {
state.clear();
state.addAll(cities);
});
});
As RxJava doesn't provide a throttle operator, you may build something similar like this :
Observable<City> limiter = Observable.zip(Observable.interval(1, SECONDS), aCity, (i, c) -> c);
Using this, limiter is an observable that will emit a city each second.
So, with your code, if you want to limit call to getCityDetail for example :
Observable<Object> limiter = Observable.interval(1, SECONDS);
stateRequest
.subscribe(State state -> {
Observable.zip(limiter, Observable.from(state.getCityList()), (i, c) -> c)
.flatMap(City city -> restService.getCityDetail(city.getId())
.toList()
.subscribe(List<City> cities -> {
state.clear();
state.addAll(cities);
});
});
stateRequest
.flatMap(new Func1<State, Observable<State>>() {
#Override
public Observable<State> call(final State state) {
List<Observable> cityObservablesList = new ArrayList<Observable>();
for(City city: state.getCityList()) {
cityObservablesList.add(restService.getCityDetail(city.getId());
}
Observable cityObservables = Observable.from(cityObservablesList);
return Observables.zip(cityObservables, new FuncN<State>() {
#Override
public State call(Object... args) {
List<City> cityList = state.getCityList();
cityList.clear();
for(Object object: args) {
cityList.add((City)object);
}
return state;
}
})
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<State>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(State result) {
// Get city list and display
}
});
I got it working with the help of zip operator and Iterable for city list as first parameter.
But i face another issue. Since the zip executes the job in parallel, 10-15 network calls are executed in parallel and server rejecting with Maximum Query Per Second error (QPS - 403).
How do i instruct the zip operator to execute the tasks one after another ?
I did solve this issue by adding a delay [delay(c*200, TimeUnit.MILLISECONDS))] to city observable. But doesn't seem like a proper solution.
Any advise ?
Take a look at flatMap(Function.., BiFunction..). Maybe that's what you need.
statesRepository.getStates()
.flatMap(states-> Observable.fromIterable(states))
.flatMap(
state-> cityRepository.getStateCities(state),
(state, cityList) -> {
state.setCities(cityList);
return state;
})
.subscribe(state-> showStateWithCity(state));
Related
I want to perform the following. I have a list of transactions which i want to update by making 2 api requests (i am using retrofit2), for each transactions and then saving the result into a database(using the observer). After some searching i decided to use zip operator to combine the 2 requests but the issue that i'm have is that i cannot identify when the whole process is finished to update the UI. Code looks like this.
for (Transaction realmTransaction : allTransactions) {
Observable<Map<String, String>> obs1 = getObs1(realmTransaction);
Observable<Map<String, String>> obs2= getObs2(realmTransaction);
Observable.zip(obs1, obs2,
(map1, map2) -> {
Map<String, String> combined = new HashMap<>();
// do some processing and return a single map after
return combined;
})
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver(realmTransaction));
}
public Observer<Map<String, String>> getObserver(Transaction t){
return new Observer<Map<String, String>>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(Map<String, String> stringStringMap) {
// update database
}
#Override
public void onError(Throwable e) {
}
#Override
public void onComplete() {
}
}
}
The observer that i have updates a field of the realmTransaction object.
My question is how do i get notified when the for loop has ended??
I would like to sent an event (maybe use EventBust) after the whole process has finish to kick off some other method.
Thanks
Also another small question that i have is about the function that i provide inside the zip operator, how can i specify on which thread that function will run on? I would like to use a computation thread for that thats why i put observeOn twice, but I couldnt find an answer anywhere
Whenever you have a for loop, you should think about range, fromArray or fromIterable. In addition, you may not need the full subscribe but doOnNext():
Observable.fromIterable(allTransactions)
.flatMap(realmTransaction -> {
Observable<Map<String, String>> obs1 = getObs1(realmTransaction);
Observable<Map<String, String>> obs2= getObs2(realmTransaction);
return Observable.zip(obs1, obs2, (map1, map2) -> {
Map<String, String> combined = new HashMap<>();
// do some processing and return a single map after
return combined;
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(stringStringMap -> handle(stringStringMap, realmTransaction));
})
.ignoreElements()
.subscribe(() -> handleCompleted(), e -> handleError(e));
Since every thing is being done asynchronously , you can create a local variable outside the loop,say count and and keep incrementing it getObserver function and later check the count is equal to the length of allTransactions.
And about your second question, I think the subscribe thread will be used. You using the 2 subscribeOn, only last one will be used.
I need to iterate through a list of data, get all their Ids, trigger network calls using those Ids, and then do something once I get the list of results (The server could take list of Ids and return a list of result but it doesn't work that way as of now).
Currently I got it working like this:
for (Data data: dataList) {
String id = data.getId();
idObservables.add(dataService.getResultFromNetwork(id));
}
Observable.zip(idObservables, new FuncN<List<Result>>() {
#Override
public List<Result> call(Object... args) {
List<Result> resultList = new ArrayList<>();
for (Object arg : args) {
resultList.add((Result) arg));
}
return resultList;
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<List<Result>>() {
#Override
public void call(List<Result> resultList) {
// Do something with the list of Result
}
}, new Action1<Throwable>() {
#Override
public void call(Throwable throwable) {
Log.e("", "error", throwable);
}
});
But obviously I'm not happy with the way it's done. It will be great to know better ways to handle a case like this using RxJava in Android.
Cheers!!
Apologies for the lambda-isms, but it really makes the logic easier to read:
Observable
.fromIterable(dataList)
.flatMap(data ->
dataService
.getResultFromNetwork(data.getId())
.subscribeOn(Schedulers.io())
)
.toList()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(list -> {
// do something
});
The idea is to keep as much as the pipeline in Rx-land; it's worth it to have simple methods taking normal parameters and return observables, and complex methods take observables and return observables.
Note: the above will not retain ordering; if you need an ordered list, use concatMap with prefetch.
I'm trying to understand how the observer pattern works in Android.
I've created this method to load a sample list of object, pushing each items to the subscriber and loading it to into the recyclerview.
I don't understand why if i load 10 items everything is working fine, but if i load 100/1000 or in general more items, the recyclerView is empty and onNext, onComplete are not fired.
private Observable<AppInfo> getAppList() {
return Observable.create(new Observable.OnSubscribe<AppInfo>() {
#Override
public void call(Subscriber<? super AppInfo> subscriber) {
for (int i = 0; i<10; i++){
AppInfo appInfo = new AppInfo(
"Test item "+i,
ContextCompat.getDrawable(getApplicationContext(), R.mipmap.ic_launcher),
i
);
subscriber.onNext(appInfo);
}
if (!subscriber.isUnsubscribed()) {
subscriber.onCompleted();
}
}
});
}
And this is how i use the Observable:
Observable<AppInfo> appInfoObserver = getAppList();
appInfoObserver
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<AppInfo>() {
#Override
public void onCompleted() {
Toast.makeText(getApplicationContext(), "App List Load Completed!", Toast.LENGTH_LONG).show();
}
#Override
public void onError(Throwable e) {}
#Override
public void onNext(AppInfo appInfo) {
if(mAppInfoList != null){
mAppInfoList.add(appInfo);
adapter.notifyItemInserted(appInfo.getAppPosition());
}
}
});
Thanks for the help and advices.
You're not logging errors so if anything goes wrong you won't know (in this case you are probably forcing a MissingBackpressureException from the observeOn operator by sending it more than it requested). To be clear, in the subscriber:
public void onError(Throwable e) {
// log or display error here!!
}
Don't use Observable.create at all if you can help it because you need to honour backpressure or combine it with .onBackpressureBuffer.
The exception is that Observable.create(new SyncOnSubscribe<T>(...)) is a good way to create an Observable if you can imagine your source as an iterator/enumeration.
To avoid using Observable.create in your example you could do this:
Observable
.range(0, 10)
.map(i -> new AppInfo(...))
or without lambda:
Observable
.range(0, 10)
.map(new Func1<Integer, AppInfo>() {
#Override
public AppInfo call(Integer n) {
return new AppInfo(...);
}
});
Maybe your code is to heavy and its loading sync. Try to load your code inside a new thread, maybe you can use the observeOn() (i dont know exactally how rxjava works, but my guess is that this function defines the thread where the event occurs).
I'm making a simple weather app to learn RxAndroid and I'm faced with the following issue.
I first load cities I'm interested in and then ask for the weather of each one of them.
getCitiesUseCase returns an Observable<List<City>> that I load from the data base. I send that list of cities to my view to display them and then ask for the weather individually (flatmap) inside the subscriber.
Subscription subscription = getCitiesUseCase.execute().flatMap(new Func1<List<City>, Observable<City>>() {
#Override
public Observable<City> call(List<City> cities) {
citiesView.addCities(cities);
return Observable.from(cities);
}
}).subscribe(new Subscriber<City>() {
#Override
public void onCompleted() {
subscriptions.remove(this);
this.unsubscribe();
}
#Override
public void onError(Throwable e) {
Log.e(this.getClass().getSimpleName(), e.toString());
}
#Override
public void onNext(City city) {
getCityWeatherUseCase.setLatLon(city.getLat().toString(), city.getLon().toString(), city.getId());
getCityWeather(city);
}
});
subscriptions.add(subscription);
Now the getCityWeather() method looks like this:
private void getCityWeather(final City city) {
subscriptions.add(getCityWeatherUseCase.execute().subscribe(new Subscriber<CityWeather>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
Log.e("error", e.toString());
}
#Override
public void onNext(CityWeather cityWeather) {
city.setCityWeather(cityWeather);
citiesView.updateCity(city);
}
}));
}
Everything works fine and as expected, but the fact that I'm subscribing to an observer inside a subcriber doesnt feel right. I know rxJava lets you play around with subscribers to prevent this kind of things but I really dont know how to improve my code further. Keep in mind that I need a city in order to ask for its weather.
Merry chrismas!
One approach could be the following. (I'm using retrolambda - so wherever you see ->, just replace with a new anonymous inner class).
Note that I'm using flatMap to spin up the weather data requests, rather than Observable.concat like your question suggests. The reason for this is that your scheduler (e.g. io()) will handle these in parallel and send the results through when they are available. However, with Observable.concat, these requests would be serialized so they'd be forced to happen one at a time - nullifying the benefits of a thread pool like io().
private class City {
public String name;
public City(String name) {
this.name = name;
}
public void setWeather(Weather weather) { /*...*/ }
}
private class Weather {
public String status;
public Weather(String status) {
this.status = status;
}
}
private Observable<Weather> getWeather(City city) {
// call your weather API here..
return Observable.just(new Weather("Sunny"));
}
#Test
public void test() {
Observable<List<City>> citiesObs = Observable.create(new Observable.OnSubscribe<List<City>>() {
#Override
public void call(Subscriber<? super List<City>> subscriber) {
// do work
final List<City> cities = new ArrayList<>();
cities.add(new City("Paris"));
cities.add(new City("Tokyo"));
cities.add(new City("Oslo"));
// send results
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(cities);
subscriber.onCompleted();
}
}
});
Observable<City> obs = citiesObs
// inject a side effect
.doOnNext(list -> {
// pass `list` to your view here
})
// turn Observable<Iterable<T>> into Observable<T>
.flatMapIterable(list -> list)
// Map a city to an observable that fetches Weather data
// Your scheduler can take care of these at once.
.flatMap(city -> {
return getWeather(city)
// another side effect
.doOnNext(weather -> {
city.setWeather(weather);
})
// map baack to city, just for the heck of it
.map($ -> city);
});
TestSubscriber sub = TestSubscriber.create();
obs.subscribe(sub);
sub.awaitTerminalEvent();
sub.assertValueCount(3);
}
Also note that in order to take advantage of io(), you'd need to add a call to subscribeOn(Schedulers.io()) to tell the observable to begin doing work on the io thread pool. When you want to pass control to another thread, for example your view, you could insert a observeOn(AndroidSchedulers.mainThread()) before your side-effect (or mapping). If you want to bounce control back to the background thread(s) for your weather calls, you could then add another call to observeOn(Schedulers.io()) right before you flatMap to getWeather(City).
Observable observable = Observable.from(backToArray(downloadWebPage("URL")))
.map(new Func1<String[], Pair<String[], String[]>>() {
#Override
public Pair<String[], String[]> call(String[] of) {
return new Pair<>(of,
backToArray(downloadWebPage("URL" + of[0])).get(0));
}
});
observable.subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(
(new Observer<Pair>() {
#Override
public void onCompleted() {
// Update user interface if needed
}
#Override
public void onError(Throwable t) {
// Update user interface to handle error
}
#Override
public void onNext(Pair p) {
offices.add(new Office((String[]) p.first, (String[]) p.second));
}
}));
This runs and i get android.os.NetworkOnMainThreadException. I would expect it to run a new thread as set by the subscribeOn() method.
Assuming that the actual network request is happening in downloadWebPage(), the error is in the first line of your code:
Observable observable = Observable.from(backToArray(downloadWebPage("http://api.ataxcloudapp.com/v1/franchise/listing/?location=" + ZIPCode)))
This is equivalent to:
String[] response = downloadWebPage("http://api.ataxcloudapp.com/v1/franchise/listing/?location=" + ZIPCode)
Observable observable = Observable.from(backToArray(response))
This should make it clear that downloadWebPage is executed - on the main thread - before any Observable is even created, let alone subscribed to. RxJava cannot change the semantics of Java in this regard.
What you can do however is something like this (not tested, but should be about right):
Observable observable = Observable.create(new Observable.OnSubscribe<String[]>() {
#Override
public void call(final Subscriber<? super String[]> subscriber) {
final String[] response = downloadWebPage("http://api.ataxcloudapp.com/v1/franchise/listing/?location=" + ZIPCode);
if (! subscriber.isUnsubscribed()) {
subscriber.onNext(backToArray(response));
subscriber.onCompleted();
}
}
)
Now your network request will happen only after the Observable is subscribed to, and will be moved to a the thread you specify in subscribeOn().
You can use defer() to postpone the calling of downloadWebPage to the moment when you subscribe to the observable.
Example:
private Object slowBlockingMethod() { ... }
public Observable<Object> newMethod() {
return Observable.defer(() -> Observable.just(slowBlockingMethod()));
}
Source
You should change from
**observable.subscribeOn(Schedulers.newThread())**
to
**observable.subscribeOn(Schedulers.io())**