首页 > 编程语言 > 详解如何使用ReactiveObjC
2021
08-12

详解如何使用ReactiveObjC

概述

RAC架构框架图

信号流程

基本使用

1、基本控件

UITextField

//监听文本输入
 [[_textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"%@",x);
 }];
  
 
//可根据自己想要监听的事件选择
 [[_textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
     NSLog(@"%@",x);
 }];
//添加条件 -- 下面表示输入文字长度 > 10 时才会调用subscribeNext
 [[_textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
     return value.length > 10;
 }] subscribeNext:^(NSString * _Nullable x) {
     NSLog(@"输入框内容:%@", x); }];

UIButton

//监听按钮点击事件
[[_btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"-->%@",x);
}];

计时器(interval、delay)

//类似timer
@weakify(self)
self.disposable = [[RACSignal interval:2 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {
        @strongify(self)
        NSLog(@"时间:%@", x); // x 是当前的时间
        //关闭计时器
        [self.disposable dispose];
}];
//延时
[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [subscriber sendNext:@"延时2秒"];
        return nil;<br>}] delay:2] subscribeNext:^(id x) {
         
        NSLog(@"-->%@",x);
}];

2、监听属性变化

//监听self的name属性
[RACObserve(self, name) subscribeNext:^(id  _Nullable x) {
        NSLog(@"属性的改变-->%@",x);
}];
[[self rac_valuesForKeyPath:@"name" observer:self] subscribeNext:^(id  _Nullable x) {
        NSLog(@"属性的改变-->%@", x);
}];
//此处RAC宏相当于让_label订阅了_textField的文本变化信号
//赋值给label的text属性
RAC(_label, text) = _textField.rac_textSignal;

3、遍历数组和字典

//遍历数组
 NSArray *array = @[@"1", @"2", @"3", @"4", @"5"];
[array.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
      NSLog(@"内容-->%@", x)<br>}];

4、监听 Notification 通知事件

[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"notification" object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        NSLog(@"-->%@", x);
}];

5、代替Delegate代理

//监听按钮点击方法的信号
//当执行完btnClickAction后会执行此订阅
[[self rac_signalForSelector:@selector(btnClickAction:)] subscribeNext:^(RACTuple * _Nullable x) {
        NSLog(@"-->%@", x);
}];
-(void) btnClickAction:(UIButton *)btn
{
    NSLog(@"按钮点击");
}

二、RAC常用类

RACSignal

RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
      [subscriber sendNext:@"🍺🍺🍺🍺🍺🍺🍺"];
      [subscriber sendCompleted];
      return [RACDisposable disposableWithBlock:^{
          NSLog(@"销毁了🍺🍺🍺");
      }];
  }];
  [signal subscribeNext:^(id  _Nullable x) {
      NSLog(@"%@",x);
  }];

RACSubject(可发送信号也可以订阅信号)

RACTuple(元组)-- 其内部就是封装了数组,用起来跟数组差不多

//通过定值创建RACTuple
RACTuple *tuple = [RACTuple tupleWithObjects:@"1", @"2", @"3", nil];
//利用 RAC 宏快速封装
RACTuple *tuple2 = RACTuplePack(@"1", @"2", @"3");
//从别的数组中获取内容
RACTuple *tuple3 = [RACTuple tupleWithObjectsFromArray:@[@"1", @"2", @"3"]];
 
NSLog(@"元组-->%@", tuple3[0]);
NSLog(@"第一个元素-->%@", [tuple3 first]);
NSLog(@"最后一个元素-->%@", [tuple3 last]);

RACMulticastConnection -- 用于当一个信号,被多次订阅时,为了保证创建信号时,避免多次调用创建信号中的block

RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
       [subscriber sendNext:@"signal1-->🍺🍺🍺🍺🍺🍺🍺"];
       [subscriber sendCompleted];
       return [RACDisposable disposableWithBlock:^{
           NSLog(@"signal1销毁了");
       }];
   }];
    
   RACMulticastConnection *connection = [signal1 publish];
    
   [connection.signal subscribeNext:^(id  _Nullable x) {
       NSLog(@"subscribeNext-->1");
   }];
   [connection.signal subscribeNext:^(id  _Nullable x) {
       NSLog(@"subscribeNext-->2");
   }];
   [connection.signal subscribeNext:^(id  _Nullable x) {
       NSLog(@"subscribeNext-->3");
   }];
   [connection connect];

RACCommand -- 可以监听信号的状态等

NSString *input = @"执行";
RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
    NSLog(@"input-->%@",input);
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"🍺🍺🍺🍺🍺🍺🍺"];
        [subscriber sendError:[NSError errorWithDomain:@"error" code:-1 userInfo:nil]];
//            [subscriber sendCompleted];
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"signal销毁了");
        }];
    }];
}];
[command.executionSignals subscribeNext:^(RACSignal   * _Nullable x) {
    NSLog(@"executionSignals-->%@",x);
    [x subscribeNext:^(id  _Nullable x) {
        NSLog(@"executionSignals-->subscribeNext-->%@",x);
    }];
}];
[[command.executionSignals switchToLatest] subscribeNext:^(id  _Nullable x) {
    NSLog(@"switchToLatest-->%@",x);
}];
[command.executing subscribeNext:^(id  _Nullable x) {
    NSLog(@"executing-->%@",x);
}];
[command.errors subscribeNext:^(id  _Nullable x) {
    NSLog(@"errors-->%@",x);
}];
//开始执行
[command execute:input];

三、高级函数使用

1、连接信号、合并信号等

首先创建两个信号signal1和signal2来演示

RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
     [subscriber sendNext:@"signal1-->🍺🍺🍺🍺🍺🍺🍺"];
     [subscriber sendCompleted];
     return [RACDisposable disposableWithBlock:^{
         NSLog(@"signal1销毁了");
     }];
 }];
 RACSignal *signal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
     [subscriber sendNext:@"signal2-->🍺🍺🍺🍺🍺🍺🍺"];
     [subscriber sendCompleted];
     return [RACDisposable disposableWithBlock:^{
         NSLog(@"signal2销毁了");
     }];
 }];

1.1、 concat -- 当多个信号发出的时候,有顺序的接收信号

一个信号signal3去监听signal1和signal2,必须是接收signal1完后才会接收signal2

RACSignal *signal3 = [signal1 concat:signal2];
  [signal3 subscribeNext:^(id  _Nullable x) {
      NSLog(@"signal3-->%@",x);
  }];

1.2、 combineLatestWith -- 将多个信号合并起来,并且拿到各个信号的最新的值,必须每个合并的signal至少都有过一次sendNext,才会触发合并的信号(订阅者每次接收的参数都是所有信号的最新值),不论触发哪个信号都会触发合并的信号

一个信号signal3去监听signal1和signal2,每次回调两个信号的最新值

RACSignal *signal3 = [signal1 combineLatestWith:signal2];
   [signal3 subscribeNext:^(id  _Nullable x) {
       NSLog(@"%@",x);
   }];

PS:注释signal1的sendNext方法,直接销毁了,所以每个singal必须有sendNext方法

1.3、 then -- 用于连接两个信号,等待第一个信号完成,才会连接then返回的信号

RACSignal *signal3 = [signal1 then:^RACSignal * _Nonnull{
    return signal2;
}];
[signal3 subscribeNext:^(id  _Nullable x) {
    NSLog(@"signal3-->%@",x);
}];

1.4、 merge -- 把多个信号合并为一个信号来监听,任何一个信号有新值的时候就会调用

一个信号signal3去监听signal1和signal2,每次回调一个信号

RACSignal *signal3 = [signal1 merge:signal2];
[signal3 subscribeNext:^(id  _Nullable x) {
    NSLog(@"signal3-->%@",x);
}];

1.5、 zipWith -- 把两个信号压缩成一个信号,只有当两个信号都发出信号内容时,才会触发

一个信号signal3去监听signal1和signal2,但必须两个信号都有发出(不需要同时,例如signal1信号发出了,signal2信号等了10秒之后发出,那么signal3的订阅回调是等signal2信号发出的那一刻触发)

RACSignal *signal3 = [signal1 zipWith:signal2];
[signal3 subscribeNext:^(id  _Nullable x) {
    NSLog(@"signal3-->%@",x);
}];

1.6、 reduce 聚合 -- 把多个信号的值按照自定义的组合返回

RACSignal *signal3 = [RACSignal combineLatest:@[signal1,signal2] reduce:^id(NSString *s1 ,NSString *s2){
    return [NSString stringWithFormat:@"%@ %@",s1,s2];
}];
[signal3 subscribeNext:^(id x) {
    NSLog(@"%@",x);
}];

2、flattenMap & map 映射

flattenMap 的底层实现是通过bind实现的

map 的底层实现是通过 flattenMap 实现的

//map事例
[[_textField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) {
        return [NSString stringWithFormat:@"%@🍺🍺🍺🍺🍺🍺🍺",value];
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"-->%@",x);
    }] ;
//flattenMap事例
 [[_textField.rac_textSignal flattenMap:^__kindof RACSignal * _Nullable(NSString * _Nullable value) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [subscriber sendNext:[NSString stringWithFormat:@"%@🍺🍺🍺🍺🍺🍺🍺",value]];
            [subscriber sendCompleted];
            return [RACDisposable disposableWithBlock:^(){}];
        }];
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"-->%@",x);
    }] ;

3、filter -- 过滤、ignore -- 忽略、distinctUntilChanged -- 忽略相同

filter、ignore

//map事例
[[_textField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) {
        return [NSString stringWithFormat:@"%@🍺🍺🍺🍺🍺🍺🍺",value];
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"-->%@",x);
    }] ;
//flattenMap事例
 [[_textField.rac_textSignal flattenMap:^__kindof RACSignal * _Nullable(NSString * _Nullable value) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [subscriber sendNext:[NSString stringWithFormat:@"%@🍺🍺🍺🍺🍺🍺🍺",value]];
            [subscriber sendCompleted];
            return [RACDisposable disposableWithBlock:^(){}];
        }];
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"-->%@",x);
    }] ;

distinctUntilChanged

RACSubject *subject = [RACSubject subject];
   [[subject distinctUntilChanged] subscribeNext:^(id  _Nullable x) {
       NSLog(@"-->%@",x);
   }];
   [subject sendNext:@"123"];
   [subject sendNext:@"123"];
   [subject sendNext:@"123"];
   [subject sendNext:@"🍺🍺🍺🍺🍺🍺"];
   [subject sendNext:@"🍺🍺🍺🍺🍺🍺"];
   [subject sendCompleted];

以上就是详解如何使用ReactiveObjC的详细内容,更多关于如何使用ReactiveObjC的资料请关注自学编程网其它相关文章!

编程技巧