question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Run two observables sequentially, using result from first observable in second observable (question)

See original GitHub issue

I’m probably overlooking something basic (sorry for that), this seems like it should be easy to do.

I have an Identifier class:

class Identifier {
    int id;
    String value;
    Ticket[] tickets;

    void setTickets(Ticket[] tickets) { this.tickets = tickets; }
}

And two separate API calls, (1) one for retrieving an identifier based on the identifier value (not the id), and (2) one for retrieving the tickets for an identifier using the identifier id:

  1. Observable<Identifier> getIdentifier(String identifierValue);
  2. Observable<Ticket[]> getTickets(int identifierId);

I want to create a method with the following signature: Observable<Identifier> getIdentifierWithTickets(String identifierValue);

that combines the two observables to create an Observable<Identifier> that:

  1. Retrieves the identifier based on the identifierValue.
  2. Retrieves the tickets for that identifier and assigns them to that identifier.

I’ve tried several operators but I can’t find the right one. The zipWith operator runs the two observables in parallel so that didn’t work. I can perhaps use flatMap but then I have to create a new observable that transforms Observable<Ticket[]> into Observable<Identifier>.

Like I said, I feel that I’m missing something very basic here, anyone please help.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
parikshitduttacommented, Jul 12, 2018

Probably we can make it a bit less convoluted, besides using map for obvious reason:

public Observable<Identifier> getIdentifierWithTickets(String identifierValue) {
     return service.getIdentifier(identifierValue)
          .flatMap(identifier -> {
               return service.getTickets(identifier.getId())
                    .map(tickets -> identifier.setTickets(tickets));
          });
}
4reactions
Jaap-van-Hengstumcommented, Feb 5, 2015

@akarnokd Thanks, I overlooked that. This simplifies things:

public Observable<Identifier> getIdentifierWithTickets(String identifierValue) {
    return service.getIdentifier(identifierValue)
            .flatMap(new Func1<Identifier, Observable<Identifier>>() {
                @Override
                public Observable<Identifier> call(final Identifier identifier) {
                    return service.getTickets(identifier.getId())
                            .map(new Func1<Ticket[], Identifier>() {
                                @Override
                                public Identifier call(Ticket[] tickets) {
                                    identifier.setTickets(tickets);
                                    return identifier;
                                }
                            });
                }
            });
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

RxJava - Run two observables sequentially, using result from ...
So far I have this but I'm unsure how to process the first observable result, like converting it to a timestamp and then...
Read more >
How can I execute two observables sequentially in ... - Reddit
all, which execute the observable in parallel, I want it to be sequential because the second one depends on the first.
Read more >
Executing RxJS 6 Observables In Order | HTMLGoodies.com
An early challenge of working with RxJS Observables is getting them to execute in a specific order. Learn how in this web dev...
Read more >
5 helpful RxJS solutions. to everyday problems | Compendium
We want to use our first observable of posts and map the result to a new observable that loads users. The operator will...
Read more >
Intro to Rx - Combining sequences - Introduction to Rx
Once the first sequence completes, the second sequence is subscribed to ... It returns three observable sequences each with a single value [1],...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found