Search code examples
iosobjective-creactive-cocoa

ReactiveCocoa subscribeNext with nil check


Most of the times when I user subscribe next, Ill check if the value isn't nil first, like so:

[[RACObserve(self.viewModel, stockViewModel.stock.imageURL) takeUntil:[self takeUntil]] subscribeNext:^(id
    value) {
        @strongify(self);

//Check if not nil
if (value) {
//Do somthing
}

    }];

Insted of doing this every time, Im trying to right a category for RACSignal that will preform this check for me, but I'm not sure how can i get the value (not the block value, the next value) from this:

- (RACDisposable *)subscribeNext:(void (^)(id x))nextBlock {
    NSCParameterAssert(nextBlock != NULL);

    RACSubscriber *o = [RACSubscriber subscriberWithNext:nextBlock error:NULL completed:NULL];
    return [self subscribe:o];
}

Any help? Thanks!


Solution

  • The ignore operation on an RACSignal can be used to filter out specific values:

    [[RACObserve(self, maybeNilProperty) ignore:nil] subscribeNext:^(id x) {
      // x can't be nil
    }];