简体   繁体   中英

How to perform chain tasks in RxJava?

I want to perform tasks in RxJava one by one.
For Example:-
1. Fetch User Ids from Server
2. Fetch Users from server by thier Ids.

I have tried this method

public Observable<List> getUids(){ return Observable.create(emitter -> { List<String> uids = new ArrayList<>(); //fetchData from server emitter.onNext(uids); }); } public Observable<User> getUser(String uid){ return Observable.create(emitter -> { User user = new User(); //fetchData user from server emitter.onNext(user); }); } //Executing this code like getUids().flatMapIterable(ids -> ids) .flatMap(this::getUser) .subscribe(new Observer<User>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(User user) { print("next "+user.getName()); } @Override public void onError(Throwable e) { print("error "+e.getMessage()); } @Override public void onComplete() { print("complete"); } });

There are some problems in it
1.this is not calling Subscriber's onComplete() method when all users are fetched.
2.if there is an error in getUser method, app is crashing. with io.reactivex.exceptions.UndeliverableException exception

Can you please tell me where I am mistaking?

  1. Call emitter.onComplete() in your getUids() and getUser(...) Observables and then append .toList() after .flatMap(this::getUser) .

    Returns a Single that emits a single item, a list composed of all the items emitted by the finite source ObservableSource.

  2. UndeliverableException is a wrapper for the exception that is happening in your .flatMap(this::getUser) . I can not help you more with the information that you provided, what is it that you want to happen when an exception is thrown?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM