美文网首页
ReactiveCocoa进阶篇

ReactiveCocoa进阶篇

作者: 马克吐温Coder | 来源:发表于2018-04-09 16:57 被阅读10次

1.rac_liftSelector:withSignals(相当于异步队列组)

{
    /**<1.创建信号1*/
    RACSignal *signal01 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        /**<4.发送信号1*/
        [subscriber sendNext:@"发送数据1"];
        return  nil;
    }];
    /**<2.创建信号2*/
    RACSignal *signal02 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        /**<5.发送信号2*/
        [subscriber sendNext:@"发送数据2"];
        return  nil;
    }];
    /**<3.加入到队列组中*/
    [self rac_liftSelector:@selector(sendMessageWithA:B:) withSignals:signal01,signal02, nil];
}
/**<6.只有加入到队列组中的信号都发送完成才会调用此方法*/
- (void)sendMessageWithA:(id)A B:(id)B{
    
}

2.信号筛选的一些方法

  • filter(过滤)

    [self.textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
        return value.length > 3;
    }];
  • ignore(忽略某些值的信号)

    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"A"];
        return nil;
    }];
    [[signal ignore:@"A"]subscribeNext:^(NSString *str) {
        NSLog(@"%@",str);
    }];
  • distinctUntilChanged(与上一次的值比较,如果相同则忽略)

    RACSubject *subject = [RACSubject subject];
    [[subject distinctUntilChanged] subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    [subject sendNext:@1];
    /**<与上一次发送的信号值相同时,被过滤*/
    [subject sendNext:@1];
  • switchToLatest(直接取到信号中的信号)

    /**<1.创建RACCommand*/
    RACCommand * command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            /**<2.发送信号*/
            [subscriber sendNext:@"我开始跑了"];

            return nil;
        }];
    }];

    /**<3.订阅RACCommand信号*/
    [command.executionSignals subscribeNext:^(RACSignal  * signal) {
        /**<4.返回的不是一个对象,而是一个信号*/
        [signal.switchToLatest subscribeNext:^(id  _Nullable x) {
            NSLog(@"%@", x);
        }];
    }];
  • take (从开始取几次信号)

    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"我跑了1"];
        [subscriber sendNext:@"我跑了2"];
        [subscriber sendNext:@"我跑了3"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    [[signal take:1] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",  x);
    }];
    
2018-04-09 15:11:24.919993+0800 ReactiveCocoa[69306:5919540] 我跑了1
2018-04-09 15:11:24.920260+0800 ReactiveCocoa[69306:5919540] 我跑了2
  • takeLast(从后开始取到第几个信号)

    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"我跑了1"];
        [subscriber sendNext:@"我跑了2"];
        [subscriber sendNext:@"我跑了3"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    [[signal takeLast:2] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",  x);
    }];
    
2018-04-09 15:12:12.268283+0800 ReactiveCocoa[69357:5921334] 我跑了2
2018-04-09 15:12:12.268645+0800 ReactiveCocoa[69357:5921334] 我跑了3
  • skip(跳过前面N次信号)

 RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"我跑了1"];
        [subscriber sendNext:@"我跑了2"];
        [subscriber sendNext:@"我跑了3"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    [[signal skip:2] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",  x);
    }];
    
 2018-04-09 15:24:20.559315+0800 ReactiveCocoa[69687:5936546] 我跑了3

3.时间控制

  • interval:(定时器)

[[RACSignal interval:3 onScheduler:[RACScheduler scheduler]]subscribeNext:^(NSDate * _Nullable x) {
        NSLog(@"我跑了");
    }];
2018-04-09 15:39:33.315067+0800 ReactiveCocoa[70143:5962467] 我跑了
2018-04-09 15:39:36.315123+0800 ReactiveCocoa[70143:5962467] 我跑了
2018-04-09 15:39:39.314985+0800 ReactiveCocoa[70143:5962467] 我跑了
2018-04-09 15:39:42.315997+0800 ReactiveCocoa[70143:5962467] 我跑了
2018-04-09 15:39:45.315919+0800 ReactiveCocoa[70143:5962467] 我跑了
  • delay (延迟执行)

    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        NSLog(@"我开始跑的时间是:%@", [NSDate date]);
        [subscriber sendNext:@"我跑了"];
        return nil;
    }];
    
    [[signal delay:3] subscribeNext:^(id  _Nullable x) {
        NSLog(@"我开始跑的时间是%@", [NSDate date]);
    }];

2018-04-09 15:43:50.259748+0800 ReactiveCocoa[70300:5970942] 我开始跑的时间是:2018-04-09 07:43:50 +0000
2018-04-09 15:43:53.264219+0800 ReactiveCocoa[70300:5970942] 我开始跑的时间是: 2018-04-09 07:43:53 +0000
  • timeout (超时)

    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        NSLog(@"外面开始派送");
        [subscriber sendNext:@"外卖到了"];
        return nil;
    }];
    
    [[[signal delay:4] timeout:2 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }error:^(NSError * _Nullable error) {
        NSLog(@"超时了,外卖还没到");
    }];
    
2018-04-09 15:53:34.852945+0800 ReactiveCocoa[70667:5990089] 外面开始派送
2018-04-09 15:53:36.853049+0800 ReactiveCocoa[70667:5990089] 超时了,外卖还没到
  • replay (当信号被多次订阅后,只执行了一次block中)

    __block int a = 0;
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        a += 1;
        [subscriber sendNext:@(a)];
        return nil;
    }];
    
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];
    
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];

2018-04-09 16:06:24.658880+0800 ReactiveCocoa[71242:6017048] 1
2018-04-09 16:06:24.659232+0800 ReactiveCocoa[71242:6017048] 1
  • retry(当不满足条件时,需要发送sendError,之后会重复执行block里面的发送,直到满足条件为止 )

    [[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        static int a = 1;
        a++;
        NSLog(@"执行");
        if (a > 4) {
            [subscriber sendNext:@"满足条件"];
        }else{
            [subscriber sendError:nil];
        }
        return  nil;
    }]retry] subscribeNext:^(id  _Nullable x) {

    }];
    
2018-04-09 16:34:42.374092+0800 ReactiveCocoa[72260:6057957] 执行
2018-04-09 16:34:42.410639+0800 ReactiveCocoa[72260:6057957] 执行
2018-04-09 16:34:42.411376+0800 ReactiveCocoa[72260:6057957] 执行
2018-04-09 16:34:42.412637+0800 ReactiveCocoa[72260:6057957] 执行
2018-04-09 16:34:42.413060+0800 ReactiveCocoa[72260:6057957] 满足条件
  • throttle节流

    [[signalBtn throttle:3] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];

4.线程控制

  • deliverOn(将信息控制在指定的线程中执行)

    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        NSLog(@"%@", [NSThread currentThread]);
        [subscriber sendNext:@"我跑了"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    signal = [signal deliverOn:[RACScheduler mainThreadScheduler]];
    
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", [NSThread currentThread]);
    }];

2018-04-09 14:54:00.999074+0800 ReactiveCocoa[68757:5891490] <NSThread: 0x60400007d500>{number = 1, name = main}
2018-04-09 14:54:00.999587+0800 ReactiveCocoa[68757:5891647] <NSThread: 0x604000464440>{number = 3, name = (null)}
  • subscribeOn (将信息和副作用控制在指定的线程中执行)

    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        NSLog(@"%@", [NSThread currentThread]);
        [subscriber sendNext:@"我跑了"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    signal = [signal subscribeOn:[RACScheduler scheduler]];
    
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", [NSThread currentThread]);
    }];

2018-04-09 14:52:30.782595+0800 ReactiveCocoa[68689:5888475] <NSThread: 0x60000026d540>{number = 3, name = (null)}
2018-04-09 14:52:30.782806+0800 ReactiveCocoa[68689:5888475] <NSThread: 0x60000026d540>{number = 3, name = (null)}

相关文章

  • ReactiveCocoa(RAC)

    一、参考 ReactiveCocoa之基础篇 ReactiveCocoa之进阶篇 函数响应式编程 Reactive...

  • ReactiveCocoa 学习

    参考:袁峥Seemygo最快让你上手ReactiveCocoa之基础篇最快让你上手ReactiveCocoa之进阶篇

  • RAC资源收集

    基本使用最快上手-基础篇最快上手-进阶篇ReactiveCocoa入门教程——第一部分ReactiveCocoa入...

  • ReactiveCocoa进阶篇

    1.rac_liftSelector:withSignals(相当于异步队列组) 2.信号筛选的一些方法 filt...

  • ReactiveCocoa进阶篇

    1.ReactiveCocoa常见操作方法介绍。 1.1 ReactiveCocoa操作须知所有的信号(RACSi...

  • ReactiveCocoa进阶篇

    1 ReactiveCocoa操作须知 所有的信号(RACSignal)都可以进行操作处理,因为所有操作方法都定义...

  • RAC常用方法

    前言 由于时间的问题,暂且只更新这么多了,后续还会持续更新本文《最快让你上手ReactiveCocoa之进阶篇》,...

  • 最快让你上手ReactiveCocoa之进阶篇

    前言 由于时间的问题,暂且只更新这么多了,后续还会持续更新本文《最快让你上手ReactiveCocoa之进阶篇》,...

  • 最快让你上手ReactiveCocoa之进阶篇

    由于时间的问题,暂且只更新这么多了,后续还会持续更新本文《最快让你上手ReactiveCocoa之进阶篇》,目前只...

  • 最快让你上手ReactiveCocoa之进阶篇

    前言由于时间的问题,暂且只更新这么多了,后续还会持续更新本文《最快让你上手ReactiveCocoa之进阶篇》,目...

网友评论

      本文标题:ReactiveCocoa进阶篇

      本文链接:https://www.haomeiwen.com/subject/iciihftx.html