ReactiveObjC使用

一.RAC基础使用

监听方法调用

[[self rac_signalForSelector:@selector(clickBtn:)] subscribeNext:^(id  _Nullable x) {
        NSLog(@"按钮点击");
    }];
    
    
    [[self rac_signalForSelector:@selector(innerFunc)] subscribeNext:^(id  _Nullable x) {
        NSLog(@"innerFunc");
    }];
    for (int i=0; i<5; i++) {
        [self innerFunc];
    }
    
    [[self rac_signalForSelector:@selector(viewDidAppear:)]subscribeNext:^(id  _Nullable x) {
        NSLog(@"viewDidAppear:%@",x);
    }];

KVO

//方式1
    [[self.view rac_valuesForKeyPath:@"backgroundColor" observer:self] subscribeNext:^(id  _Nullable x) {
        NSLog(@"backgroundColor1:%@",x);
    }];
    self.view.backgroundColor=[UIColor orangeColor];
    
    //方式2
    [RACObserve(self.view,backgroundColor)subscribeNext:^(id  _Nullable x) {
        NSLog(@"backgroundColor2:%@",x);
    }];
    self.view.backgroundColor=[UIColor whiteColor];

通知

[[[NSNotificationCenter defaultCenter]rac_addObserverForName:UIKeyboardDidShowNotification object:self]subscribeNext:^(NSNotification * _Nullable x) {
       
        NSLog(@"监听键盘弹出通知x:%@",x);
        
    }];

监听View事件

[[self.btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        NSLog(@"监听Button点击:x:%@",x);
    }];
    
    [[self.textfield rac_textSignal]subscribeNext:^(NSString * _Nullable x) {
        
        NSLog(@"监听TextField输入变化:x:%@",x);
    }];

监听Timer

__block int i=0;
    racdis =[[RACSignal interval:1.0 onScheduler:[RACScheduler scheduler]]subscribeNext:^(NSDate * _Nullable x) {
       
        NSLog(@"rac_tiemr:%@",x);
        i++;
        if(i==5)
        {
            [racdis dispose];
        }
        
    }];

RACSignal

RACSignal可以作为消息订阅者,但是自身不能发送消息。

RACSignal * signal=[RACSignal createSignal:^RACDisposable *(id subscriber) {
        [subscriber sendNext:@"signal"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    [signal subscribeNext:^(id x) {
        SDLog(@"signal_x:%@",x);
    }];

RACSubject

RACSubject是RACSignal的子类
RACSignal可以作为消息订阅者,同时也可以发送消息。

RACSubject * subject=[RACSubject subject];
    [subject subscribeNext:^(id x) {
        SDLog(@"subject_x:%@",x);
    }];
    [subject sendNext:@"subject"];

RACCommand

//1.创建信号
RACCommand * command=[[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal createSignal:^RACDisposable *(id subscriber) {
            [subscriber sendNext:@"command"];
            [subscriber sendCompleted];
            return nil;
        }];
    }];
 //2.订阅信号
 [command.executionSignals.switchToLatest subscribeNext:^(id x) {
        SDLog(@"command_x:%@",x);
    }];
 //3.执行信号
 [command execute:nil];

二.RAC高级使用

distinctUntilChanged:

当上一次的值和当前的值有明显的变化就会发出信号,否则会被忽略掉。
在开发中,刷新UI经常使用,只有两次数据不一样才需要刷新

RACSubject * subject=[RACSubject subject];
    [subject subscribeNext:^(id x) {
        SDLog(@"x=%@",x);//打印x=1 x=2 x=3 x=3 x=3
    }];
//    [[subject distinctUntilChanged] subscribeNext:^(id x) {
//        SDLog(@"x=%@",x);//打印x=1 x=2 x=3
//    }];
    [subject sendNext:@"1"];
    [subject sendNext:@"2"];
    [subject sendNext:@"3"];
    [subject sendNext:@"3"];
    [subject sendNext:@"3"];

take:

从开始一共取N次的信号

RACSubject * subject=[RACSubject subject];
    //take:取前面几个值
    //在没遇到[subject sendCompleted];那么就会停止发送信号
    [[subject take:2]subscribeNext:^(id x) {
        SDLog(@"take %@",x);//打印1 2
    }];
    //takeLast:取后面多少个值,必须发送完成
    //只有[subject sendCompleted];才会发送信号
    [[subject takeLast:2]subscribeNext:^(id x) {
        SDLog(@"takeLast %@",x);//打印4 5
    }];
    
    //takeUntil:只要传入的信号发送完成或者self.rac_willDeallocSignal发送信号,就不会再接收信号的内容
    [[subject takeUntil:self.rac_willDeallocSignal]subscribeNext:^(id x) {
        SDLog(@"takeUntil %@",x);//打印1 2 3 4 5
    }];
    [subject sendNext:@"1"];
    [subject sendNext:@"2"];
    [subject sendNext:@"3"];
    [subject sendNext:@"4"];
    [subject sendNext:@"5"];
    [subject sendCompleted];

skip:

跳过前几个信号再开始接受信号

RACSubject * subject=[RACSubject subject];
    [[subject skip:3]subscribeNext:^(id x) {
        SDLog(@"%@",x);//打印4 5
    }];
    [subject sendNext:@"1"];
    [subject sendNext:@"2"];
    [subject sendNext:@"3"];
    [subject sendNext:@"4"];
    [subject sendNext:@"5"];
    [subject sendCompleted];

switchToLatest:

用于signalOfSignals(信号的信号),有时候信号也会发出信号,会在signalOfSignals中,获取signalOfSignals发送的最新信号

RACSubject * subject=[RACSubject subject];
    RACSubject * subject_sub1=[RACSubject subject];
    [subject.switchToLatest subscribeNext:^(id x) {
        SDLog(@"%@",x);//打印1 2
    }];
    [subject sendNext:subject_sub1];
    [subject_sub1 sendNext:@"1"];
    [subject_sub1 sendNext:@"2"];

你可能感兴趣的:(ReactiveObjC使用)