1

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));
}
Morteza Rastgoo
  • 6,178
  • 6
  • 35
  • 55
  • What is "strings"? If this your original urls or a collection of your return value from upload(String url)? – Will Aug 16 '15 at 07:46
  • Is this question still unanswered? According to your comments it seems like Dave's answer should be accepted unless theres something else. – Aaron Aug 21 '15 at 20:59
  • yes, the problem is answered but Dave Moten said he will edit his answer, but he hasn't edited yet. – Morteza Rastgoo Aug 26 '15 at 05:57

1 Answers1

4

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));
Dave Moten
  • 11,689
  • 2
  • 35
  • 44
  • The problem is not the list! The problem is that doOnCompleted() cannot return an observable, so i can continue the flow... – Morteza Rastgoo Aug 16 '15 at 04:05
  • What happens if i try to use Flatmap instead of doOnCompleted? Does it react the same? – Morteza Rastgoo Aug 16 '15 at 04:27
  • ok , so b does not take the stream as a parameter but is independent and returns its own observable, it's just that you want it to run after the uploads have happened? I'll update the answer then. – Dave Moten Aug 16 '15 at 05:28
  • Ok I've updated it with my guess. Can you refine the question so we know what the signature of b is for starters and where strings comes from? – Dave Moten Aug 16 '15 at 05:33
  • Your previous answer helped me alot. toList() makes it possible to emit all items in Observable.from(urls) .flatMap(url -> upload(url)) .toList() and then i can Do Other things like flatmap after this state . – Morteza Rastgoo Aug 17 '15 at 12:24
  • 1
    ok good. Yeah toList works if you need to do stuff with all the emissions but that wasn't clear from the question. Should I leave the answer as is or shall we clean up question and answer? – Dave Moten Aug 17 '15 at 22:45
  • I appreciate if you do that. thank you! I also have another question here if you can answer: http://stackoverflow.com/questions/32063496/rx-java-pass-by-reference-or-pass-by-value – Morteza Rastgoo Aug 18 '15 at 04:26