简体   繁体   中英

Signal of Signals ? Reactive Cocoa

I am trying to understand this piece of code and why it returns a signal of signals.

[[[self.signInButton
   rac_signalForControlEvents:UIControlEventTouchUpInside]
   map:^id(id x) {
     return [self signInSignal];
   }]
   subscribeNext:^(id x) {
     NSLog(@"Sign in result: %@", x);
   }];

From what I understand the UIControlEventTouchupInside returns a next signal which contains the UIButton when unwrapped in the map function.

So from this, is it safe to conclude that when I perform the map, it expects that the object returned is an object (which happens to be a signal) and it double wraps the signal into a signal. That is reactive cocoa usually expects the type to be a object type and wrap's it into a signal ?

Or

Am I missing something here, and my understanding of Map is not as follows, It transforms the input value, and produces an output value, (independent of the input value sometimes)

Thanks I am new to this any advice would be nice!

Another related question,

Lets just say that uicontrol event touch up inside sends and error event, would it be caught by the subscribe next error ?

The code looks incorrect. You are correct in understanding that the code takes the signal of button taps and maps it to a signal of "sign-in signals". Yes, a signal of signals. While a signal of signals is common and very useful, it will do nothing without the use of one of these three operators: flatten , concat or switchToLatest . In the given code, signInSignal will never be triggered because none of these operators are used.

If you switch map: to flattenMap: , the sign-in signal will be properly started.

[[[self.signInButton
    rac_signalForControlEvents:UIControlEventTouchUpInside]
    flattenMap:^id(id x) {
        return [self signInSignal];
    }]
    subscribeNext:^(id x) {
        NSLog(@"Sign in result: %@", x);
    }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM