Reputation: 693
So I'm quite new to Reactive programming, after following this tuto I'm able to send multi-threaded REST requests like so:
Retrofit2 interface:
@FormUrlEncoded
@PUT("path")
Observable<String> putMethod(
@Field("field") String field,
...
);
Retrofit2 builder:
private NetRequestService() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(END_POINT)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
service = retrofit.create(RetrofitInterface.class);
}
Request:
public void putMethod(Context context, List<Object> objectList){
for(Object o: objectList) {
service.putMethod(
"field",
...
)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<String>() {
@Override
public final void onCompleted() {
}
@Override
public final void onError(Throwable e) {
Log.e("Error", e.getMessage());
}
@Override
public final void onNext(String response) {
Log.e("Response", response);
}
});
}
}
Everything is working fine, the problem is that the onCompleted() method is called for each call (as expected) but I'd like to know when all calls to service.putMethod(...) are completed. How can I do that? I'd like to have some sort of callback when all calls are done to perform another action.
Thanks
Upvotes: 0
Views: 1524
Reputation: 29260
You can do this
Observable
.fromIterable(list)
.flatMap(object -> service.putMethod(/* ... */))
.ignoreElements()
.subscribe(/* ... */)
This will discard the results though, you will only get the onComplete. If you're interested in the results, you could put them in a List or some other object:
Observable
.fromIterable(list)
.flatMap(object -> service.putMethod(/* ... */))
.toList()
.subscribe(/* ... */)
Upvotes: 3