RAC using (a) - Keyword

This RAC usually used to sum up a few keywords (method). RAC is to be understood that processing of the data stream.

Create signal - processing pipeline understood the data stream

Creates a signal for transmitting a character string: "transmitting a first signal", a signal can be understood as a conduit.

RACSignal *signalOne = [RACSignal
                            createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                                
                                //发送信号
                                [subscriber sendNext:@"发送第一个信号"];
                                [subscriber sendCompleted];
                                
                                return [RACDisposable disposableWithBlock:^{
                                    
                                }];
                            }];
复制代码

Create a signal 2 signalTwo, sending three signals.

RACSignal *signalTwo = [RACSignal
                            createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                                
                                //发送信号
                                [subscriber sendNext:@"发送第二个信号1"];
                                [subscriber sendNext:@"发送第二个信号2"];
                                [subscriber sendNext:@"发送第二个信号3"];
                                
                                [subscriber sendCompleted];
                                
                                return [RACDisposable disposableWithBlock:^{
                                    
                                }];
                            }];
复制代码

flattenMap

By flattenMap, will generate a new signal. Which can process data. Here, I cut the string signal 1 of 3. I.e. by flattenMap, flows into the pipe inside the data stream is converted into a new data stream.

signalOne = [signalOne flattenMap:^RACStream *(id value) {

        if (![value isKindOfClass:[NSString class]]) {
            return [RACSignal error:[NSError new]];
        }
        
        NSString *valueStr = (NSString *)value;
        if (valueStr.length >= 3) {
            return [RACSignal return:[valueStr substringToIndex:3]];
        }
        return [RACSignal return:valueStr];
    }];
    
    [signalOne subscribeNext:^(id x) {
        NSLog(@"===%@", x);
    }];
复制代码

map

And flattenMap different, where you can also do data manipulation, but does not return the amount of the new signal, but directly to data manipulation.

[[signalOne map:^id(id value) {
        
        if (![value isKindOfClass:[NSString class]]) {
            return value;
        }
        
        NSString *valueStr = (NSString *)value;
        if (valueStr.length >= 3) {
            return [valueStr substringToIndex:3];
        }
        return valueStr;
    }] subscribeNext:^(id x) {
        NSLog(@"===%@", x);
    }];
复制代码

filter

2 three signals transmitted character string, only when the "second signal transmission 2" will be sent to. Used to determine the length of the input text box.

[[signalTwo filter:^BOOL(id value) {
        
        //此处return Yes才走subscribeNext。
        if ([value isEqualToString:@"发送第二个信号2"]) {
            return YES;
        }else
        {
            return NO;
        }
    }]subscribeNext:^(id x) {
        
        NSLog(@"%@",x);
    }];
复制代码

take

The number after the take, showing several signals before transmission. Here only the first two transmission signals 2.

[[signalTwo
      take:2]
     subscribeNext:^(id x) {
         NSLog(@"%@",x);
     }];
复制代码

takeLast

takeLast, denotes a transmission signal of the last few. Here only the last transmission signal of the two signals 2.

[[signalTwo
      takeLast:2]
     subscribeNext:^(id x) {
         NSLog(@"%@",x);
     }];
复制代码

ignore

With the latter ignore the value. Where said signal "transmitting a second signal 2" are not sent 2.

[[signalTwo
      ignore:@"发送第二个信号2"]
     subscribeNext:^(id x) {
         NSLog(@"%@",x);
     }];
复制代码

At last

These are a few relatively simple method, as well as merge, rac_sequence etc. - written wrong, please exhibitions ~ Gangster article did not write a few examples of the results, interested students can try their own ~ I wish you all have fun

Reproduced in: https: //juejin.im/post/5d061f29e51d4510624f97ca

Guess you like

Origin blog.csdn.net/weixin_33840661/article/details/93180819