4

With ReactiveCocoa I'm sure there is a better way than doing this?

RACSignal *crashSignal = [cancelSignal tryMap:^id(id value, NSError **errorPtr) {
    *errorPtr = [self createError];
    return nil;
}];
hfossli
  • 22,616
  • 10
  • 116
  • 130

1 Answers1

11

More idiomatic would be:

RACSignal *crashSignal = [cancelSignal flattenMap:^(id value) {
    return [RACSignal error:[self createError]];
}];
joshaber
  • 2,465
  • 20
  • 13
  • I've never really understood `flattenMap`. Would you care to say a few lines about when it is useful and why it is used widely throughout RAC? – hfossli Feb 12 '14 at 15:29
  • 4
    @hfossli `-flattenMap:` allows you to map from a _value_ to a _signal_. The resulting signals (from mapping each value in the original) will be merged together automatically. For the case you're describing, injecting an `NSError` _value_ into the signal won't actually terminate it, so you actually want to map to an _error signal_ that will terminate the whole stream as soon as it gets merged in. – Justin Spahr-Summers Feb 12 '14 at 17:09
  • Makes perfect sense now!! Thanks a lot! – hfossli Feb 12 '14 at 21:35