ReactiveCocoa进阶篇

2018-04-09  本文已影响10人  马克吐温Coder

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.信号筛选的一些方法

    [self.textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
        return value.length > 3;
    }];
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"A"];
        return nil;
    }];
    [[signal ignore:@"A"]subscribeNext:^(NSString *str) {
        NSLog(@"%@",str);
    }];
    RACSubject *subject = [RACSubject subject];
    [[subject distinctUntilChanged] subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    [subject sendNext:@1];
    /**<与上一次发送的信号值相同时,被过滤*/
    [subject sendNext:@1];
    /**<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);
        }];
    }];
    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
    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
 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.时间控制

[[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] 我跑了
    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
    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] 超时了,外卖还没到
    __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
    [[[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] 满足条件
    [[signalBtn throttle:3] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];

4.线程控制

    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)}
    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)}
上一篇 下一篇

猜你喜欢

热点阅读