ReactiveCocoa入门到实战 学习笔记(二)

信号的高阶操作


内容大纲

  • 信号的高阶操作

信号的高阶操作

  1. 升阶和降阶
    我们可以将一个返回 Value 的信号升阶成一个返回 Signal 的信号,这种行为称之为升阶
    我们也可以将一个返回 Signal 的信号降阶成一个返回 Value 的信号,这种行为称之为降阶
    // 升阶
    RACSignal *arraySignal = [@[@1, @2, @3, @4, @5, @6, @8, @10, @0].rac_sequence.signal map:^RACStream *(id value) {
        
        return [RACSignal return:value];
    }];

    // 降阶
    [[arraySignal flatten] subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
  1. 降阶操作
  • switchToLatests
    RACSignal *signal1 = [[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] mapReplace:[RACSignal return:@1]];
    
    RACSignal *signal4 = [RACSignal return:signal1];
    
    RACSignal *signal2 = [[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] mapReplace:[RACSignal return:@"hello"]];
    
    RACSignal *signal3 = [[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] mapReplace:signal2];
    
    [[[[signal4 merge:signal3] switchToLatest] flatten] subscribeNext:^(id x) {
       
        NSLog(@"%@", x);
    }];

signal4 是一个包含了一个定时器信号的信号,signal3 是按钮的点击事件触发,也包含的是一个定时器信号,当点击按钮时,原本定时打印1的结果改变为定时打印hello。switchLatests表示,当下一个信号来临时,取消前一个信号的订阅,转而订阅下一个信号。

  • If/then/else
    RACSignal *ifSignal = [[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] scanWithStart:@YES reduce:^id(id running, id next) {
        
        return [running boolValue] ? @NO : @YES;
    }];
    
    RACSignal *yesSignal = [RACSignal return:@"YES"];
    RACSignal *noSignal = [RACSignal return:@"NO"];
    
    [[RACSignal if:ifSignal then:yesSignal else:noSignal] subscribeNext:^(id x) {
       
        NSLog(@"%@", x);
    }];
    
    /*
     2021-08-04 16:40:22.179744+0800 RACTestOne[38424:1864836] NO
     2021-08-04 16:40:23.061356+0800 RACTestOne[38424:1864836] YES
     2021-08-04 16:40:23.751064+0800 RACTestOne[38424:1864836] NO
     2021-08-04 16:40:24.335030+0800 RACTestOne[38424:1864836] YES
     2021-08-04 16:40:24.862587+0800 RACTestOne[38424:1864836] NO
     2021-08-04 16:40:25.186937+0800 RACTestOne[38424:1864836] YES
     2021-08-04 16:40:25.495577+0800 RACTestOne[38424:1864836] NO
     */
  • switch/case
    根据switch订阅对应的signal
    RACSignal *swithSignal_1 = [RACSignal return:@"1"];
    RACSignal *swithSignal_2 = [RACSignal return:@"2"];
    RACSignal *swithSignal_3 = [RACSignal return:@"3"];
    
    [[RACSignal switch:swithSignal_1 cases:@{@"1": swithSignal_2} default:swithSignal_3] subscribeNext:^(id x) {
       
        NSLog(@"%@", x);
    }];
    
    /*
     2021-08-04 16:44:57.056471+0800 RACTestOne[38484:1868134] 3
     */
  • flatten
    根据时间将多个信号异步执行
    RACSignal *signal1 = [[[[[RACSignal interval:2 onScheduler:[RACScheduler mainThreadScheduler]] scanWithStart:@0 reduce:^id(id running, id next) {
        
        return @([running integerValue] + 2);
    }] take:5] map:^id(id value) {
        return [RACSignal return:value];
    }] delay:1];
    
    RACSignal *signal2 = [[[[RACSignal interval:2 onScheduler:[RACScheduler scheduler]] scanWithStart:@(-1) reduce:^id(id running, id next) {
        
        return @([running integerValue] + 2);
    }] take:5] map:^id(id value) {
        return [RACSignal return:value];
    }];
    
    [[[signal1 merge:signal2] flatten] subscribeNext:^(id x) {
       
        NSLog(@"%@ -- %@", x, [NSThread currentThread]);
    } completed:^{
        NSLog(@"finished");
    }];
    
    /*
     2021-08-04 16:52:48.891416+0800 RACTestOne[38662:1875819] 1 -- {number = 6, name = (null)}
     2021-08-04 16:52:49.983304+0800 RACTestOne[38662:1875744] 2 -- {number = 1, name = main}
     2021-08-04 16:52:50.891148+0800 RACTestOne[38662:1875812] 3 -- {number = 7, name = (null)}
     2021-08-04 16:52:51.991282+0800 RACTestOne[38662:1875744] 4 -- {number = 1, name = main}
     2021-08-04 16:52:52.890137+0800 RACTestOne[38662:1875817] 5 -- {number = 3, name = (null)}
     2021-08-04 16:52:53.923718+0800 RACTestOne[38662:1875744] 6 -- {number = 1, name = main}
     2021-08-04 16:52:54.890292+0800 RACTestOne[38662:1875819] 7 -- {number = 6, name = (null)}
     2021-08-04 16:52:55.966818+0800 RACTestOne[38662:1875744] 8 -- {number = 1, name = main}
     2021-08-04 16:52:56.891232+0800 RACTestOne[38662:1875817] 9 -- {number = 3, name = (null)}
     2021-08-04 16:52:57.951822+0800 RACTestOne[38662:1875744] 10 -- {number = 1, name = main}
     2021-08-04 16:52:57.952246+0800 RACTestOne[38662:1875744] finished
     */
  • flattenMap
    等于先map,后flatten,信号的返回值必须是一个信号,订阅的时候会被降阶成一个值,等于先使用map,后使用flatten
  • try

你可能感兴趣的:(ReactiveCocoa入门到实战 学习笔记(二))