I want to send multiple requests over the network and this tutorial helped but i'm stuck at the latter part . seems i'm expected to return a value(OrderValues) from onSubscribe,onNext,.... since apply function returns a value. But ....,onNext returns void by default. Any help?Here is my piece of code
Observable<Restaurant> orderRestaurant= IdentityClient.getAPIService()
.getRestaurantById(restaurantId)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Observable<Menu> orderMenu= IdentityClient.getAPIService()
.getMenuById(menuId)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Observable<User> orderUser= IdentityClient.getAPIService()
.getUserById(userId)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Observable<OrderValues> combineValues=Observable.zip(orderRestaurant,
orderMenu, orderUser,
new Function3<Restaurant, Menu, User, OrderValues>() {
@Override
public OrderValues apply(Restaurant restaurant, Menu menu, User user)
throws Exception {
return new OrderValues(restaurant,menu,user);
}
I get an error here "cannot resolve method 'subscribe anonymous
org.reactivestreams.Subscriber(....OrderValues)
}).subscribe(new Subscriber<OrderValues>() {
@Override
public void onSubscribe(Subscription s) {
}
@Override
public void onNext(OrderValues orderValues) {
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
}
});
I'm assuming that you are using RxJava 2.
Use Observer
instead of Subscriber
. And also do not assign the result to a new Observable (you called it combineValues).
private void myMethod() {
Observable.zip(orderRestaurant, orderMenu, orderUser, new Function3<Restaurant, Menu, User, OrderValues>() {
@Override
public OrderValues apply(@NonNull Restaurant restaurant, @NonNull Menu menu, @NonNull User user) throws Exception {
return new OrderValues(restaurant, menu, user);
}
}).subscribe(new Observer<OrderValues>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(OrderValues orderValues) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}