Search code examples
iosobjective-creactive-programmingreactive-cocoa

Removing nested subscribeNext in reactive pipeline


I am trying to construct a reactive pipeline where a change in some input signals will trigger off an asynchronous web api request. But I do not need to perform anything in the subscribeNext block of the web api.

This is how it looks:

RACSignal *inputChanged = [[RACSignal merge:@[ RACObserve(self, input1), RACObserve(self, input2) ]] throttle:0.5];

@weakify(self)
[[[inputChanged
  subscribeNext:^(id x) {
      @strongify(self)
      [[self asyncWebAPI]
       subscribeNext:^(id x) {
       }];
  }];

This works. But it isn't elegant because of the nested subscribeNext: and the second subscribeNext is just to make the asyncWebAPI a hot signal.

Is there a better way to construct this pipeline?


Solution

  • Check out -flattenMap:. For example:

    [[inputChanged
        flattenMap:^(id _) {
            @strongify(self);
            return [self asyncWebAPI];
        }]
        subscribeNext:^(id x) {
            // Do stuff
        }];