Morteza Rastgoo
Morteza Rastgoo

Reputation: 6996

How to return observable after all values of first observable are emmitted?

I have a method that creates an observable from list of strings.

public Observable makeUrls() {
    List<String> urls = new ArrayList<>();
    return Observable.from(urls)
            .flatMap(url -> upload(url));
}

Now I want to return method b after all values in the list is emitted.

public Observable b(List<String> strings){
    return Observable.from(strings)
                     ..some other work here...;
}

The expected code i need is something like this:

public Observable makeUrls() {
    List<String> urls = new ArrayList<>();
    return Observable.from(urls)
            .flatMap(url -> upload(url))
            // This part is what i can't figure out how to write...
            // I want to call this after all items are emitted but I can't return Observable
            .doOnCompleted(()->b(strings));
}

Upvotes: 0

Views: 676

Answers (1)

Dave Moten
Dave Moten

Reputation: 12097

Use .ignoreElements and concatWith:

Suppose b(strings) returns Observable<Thing>:

return Observable.from(urls)
        .flatMap(url -> upload(url))
        .ignoreElements()
        .castAs(Thing.class)
        .concatWith(b(strings));

Upvotes: 4

Related Questions