ReactiveObjC看这里就够了

作者 : 开心源码 本文共6983个字,预计阅读时间需要18分钟 发布时间: 2022-05-12 共206人阅读

1、什么是ReactiveObjC

ReactiveObjC是ReactiveCocoa
系列的一个OC方面用得很多的响应式编程三方框架,其Swift方面的框架是(ReactiveSwift)。RAC用信号(类名为RACSignal)来代替和解决各种变量的变化和传递。核心思路:创立信号->订阅信号(subscribeNext)->发送信号
通过信号signals的传输,重新组合和响应,软件代码的编写逻辑思路将变得更清晰紧凑,有条理,而不再需要对变量的变化不断的观察升级。

2、什么是函数响应式编程

响应式编程是一种和事件流有关的编程模式,关注导致状态值改变的改变的行为事件,一系列事件组成了事件流,一系列事件是导致属性值发生变化的起因,非常相似于设计模式中的观察者模式。在网上流传一个非常经典的解释响应式编程的概念,在一般的程序开发中:a = b + c,赋值之后 b 或者者 c 的值变化后,a 的值不会跟着变化,而响应式编程的目标就是,假如 b 或者者 c 的数值发生变化,a 的数值会同时发生变化;

3、ReactiveObjC的流程分析

ReactiveObjC主要有三个关键类:
1、RACSignal信号
RACSignal 是各种信号的基类,其中RACDynamicSignal是用的最多的动态信号
2、RACSubscriber订阅者
RACSubscriber是实现了RACSubscriber协议的订阅者类,这个协议定义了4个必需实现的方法

@protocol RACSubscriber <NSObject>@required- (void)sendNext:(nullable id)value;              //常见- (void)sendError:(nullable NSError *)error;  //常见- (void)sendCompleted;                                //常见- (void)didSubscribeWithDisposable:(RACCompoundDisposable *)disposable;@end

RACSubscriber主要保存了三个block,跟三个常见的协议方法逐个对应

@property (nonatomic, copy) void (^next)(id value);@property (nonatomic, copy) void (^error)(NSError *error);@property (nonatomic, copy) void (^completed)(void);

3、RACDisposable清洁工
RACDisposable主要是对资源的释放解决,其中使用RACDynamicSignal时,会创立一个RACCompoundDisposable管理清洁工对象。其内部定义了两个数组,一个是_inlineDisposables[2]固定长度2的A fast array,超出2个对象的长度由_disposables数组管理,_inlineDisposables数组速度快,两个数组都是线程安全的。

4、ReactiveObjC导入工程的方式

pod 'ReactiveObjC'

5、ReactiveObjC的几种使用情况

  • NSArray 数组遍历
    NSArray * array = @[@"1",@"2",@"3",@"4",@"5",@"6"];    [array.rac_sequence.signal subscribeNext:^(id  _Nullable x) {        NSLog(@"数组内容:%@", x);    }];
  • NSArray快速替换数组中内容为99和单个替换数组内容,两个方法都不会改变原数组内容,操作完后都会生成一个新的数组,省去了创立可变数组而后遍历出来单个增加的步骤。
   NSArray * array = @[@"1",@"2",@"3",@"4",@"5",@"6"];    /*     NSArray * newArray = [[array.rac_sequence mapReplace:@"99"] array];     NSLog(@"%@",newArray);     */    NSArray * newArray = [[array.rac_sequence map:^id _Nullable(id  _Nullable value) {        NSLog(@"原数组内容%@",value);        return @"99";    }] array];    NSLog(@"%@",newArray);
  • NSDictionary 字典遍历
    NSDictionary * dic = @{@"name":@"Tom",@"age":@"20"};    [dic.rac_sequence.signal subscribeNext:^(id  _Nullable x) {            RACTupleUnpack(NSString *key, NSString * value) = x;//X为为一个元祖,RACTupleUnpack能够将key和value区分开        NSLog(@"数组内容:%@--%@",key,value);    }];
  • UIButton 监听按钮的点击事件
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];    btn.frame = CGRectMake(100, 200, 100, 60);    btn.backgroundColor = [UIColor blueColor];    [self.view addSubview:btn];    //监听点击事件    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {        NSLog(@"%@",x);//x为一个button对象,别看类型为UIControl,继承关系UIButton-->UIControl-->UIView-->UIResponder-->NSObject    }];
  • UITextField 监听输入框的少量事件
    UITextField * textF = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];    textF.placeholder = @"请输入内容";    textF.textColor = [UIColor blackColor];    [self.view addSubview:textF];    //实时监听输入框中文字的变化    [[textF rac_textSignal] subscribeNext:^(NSString * _Nullable x) {        NSLog(@"输入框的内容--%@",x);    }];    //UITextField的UIControlEventEditingChanged事件,免去了KVO    [[textF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {        NSLog(@"%@",x);    }];    //增加监听条件    [[textF.rac_textSignal filter:^BOOL(NSString * _Nullable value) {        return [value isEqualToString:@"100"];//此处为判断条件,当输入的字符为100的时候执行下面方法    }]subscribeNext:^(NSString * _Nullable x) {        NSLog(@"输入框的内容为%@",x);    }];
  • KVO 代替KVO来监听按钮frame的改变
    UIButton * loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];    loginBtn.frame = CGRectMake(100, 210, 100, 60);    loginBtn.backgroundColor = [UIColor blueColor];    [loginBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];    [loginBtn setTitle:@"666" forState:UIControlStateNormal];    //[loginBtn setTitle:@"111" forState:UIControlStateDisabled];    [self.view addSubview:loginBtn];    //监听点击事件    [[loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {        NSLog(@"%@",x);//x为一个button对象,别看类型为UIControl,继承关系UIButton-->UIControl-->UIView-->UIResponder-->NSObject        x.frame = CGRectMake(100, 210, 200, 300);    }];    //KVO监听按钮frame的改变    [[loginBtn rac_valuesAndChangesForKeyPath:@"frame" options:(NSKeyValueObservingOptionNew) observer:self] subscribeNext:^(RACTwoTuple<id,NSDictionary *> * _Nullable x) {        NSLog(@"frame改变了%@",x);    }];        //下面方法也能监听,但是在按钮创立的时候此方法也执行了,简单说就是在界面展现之前此方法就走了一遍,总感觉怪怪的。   /*    [RACObserve(loginBtn, frame) subscribeNext:^(id  _Nullable x) {    NSLog(@"frame改变了%@",x);    }];
  • NSNotification 监听通知事件
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {        NSLog(@"监听键盘弹出"); //不知道为啥此方法不止走一次,但是本来的通知监听方法只走一次,有知道的可以私信我,谢谢    }];
  • timer 代替timer定时循环执行方法
[[RACSignal interval:2.0 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {        //这里面的方法2秒一循环    }];//假如关闭定时器,中止需要创立一个全局的disposable//@property (nonatomic, strong) RACDisposable * disposable;//创立/*self.disposable = [[RACSignal interval:2.0 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {        NSLog(@"当前时间:%@", x); // x 是当前的系统时间        //关闭计时器        [self.disposable dispose];    }];*/

6、开发中用到的小栗子

  • 发送短信验证码的按钮倒计时
/*     @property (nonatomic, strong) RACDisposable * disposable;     @property (nonatomic, assign) NSInteger time;     */    //上面两句要提前定义    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 200, 50)];    btn.titleLabel.textAlignment = NSTextAlignmentCenter;    btn.backgroundColor = [UIColor greenColor];    [btn setTitle:@"发送验证码" forState:(UIControlStateNormal)];    [self.view addSubview:btn];    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {        self.time = 10;        btn.enabled = NO;        [btn setTitle:[NSString stringWithFormat:@"请稍等%zd秒",self.time] forState:UIControlStateDisabled];        self.disposable = [[RACSignal interval:1.0 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {            //减去时间            self.time --;            //设置文本            NSString *text = (self.time > 0) ? [NSString stringWithFormat:@"请稍等%zd秒",_time] : @"重新发送";            if (self.time > 0) {                btn.enabled = NO;                [btn setTitle:text forState:UIControlStateDisabled];            }else{                btn.enabled = YES;                [btn setTitle:text forState:UIControlStateNormal];                //关掉信号                [self.disposable dispose];            }        }];    }];
  • 登录按钮的状态根据账号和密码输入框内容的长度来改变
UITextField *userNameTF = [[UITextField alloc]initWithFrame:CGRectMake(100, 70, 200, 50)];    UITextField *passwordTF = [[UITextField alloc]initWithFrame:CGRectMake(100, 130, 200, 50)];    userNameTF.placeholder = @"请输入客户名";    passwordTF.placeholder = @"请输入密码";    [self.view addSubview:userNameTF];    [self.view addSubview:passwordTF];    UIButton *loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(40, 180, 200, 50)];    [loginBtn setTitle:@"马上登录" forState:UIControlStateNormal];    [self.view addSubview:loginBtn];    //根据textfield的内容来改变登录按钮的点击可否    RAC(loginBtn, enabled) = [RACSignal combineLatest:@[userNameTF.rac_textSignal, passwordTF.rac_textSignal] reduce:^id _Nullable(NSString * username, NSString * password){        return @(username.length >= 11 && password.length >= 6);    }];    //根据textfield的内容来改变登录按钮的背景色    RAC(loginBtn, backgroundColor) = [RACSignal combineLatest:@[userNameTF.rac_textSignal, passwordTF.rac_textSignal] reduce:^id _Nullable(NSString * username, NSString * password){        return (username.length >= 11 && password.length >= 6) ? [UIColor redColor] : [UIColor grayColor];    }];

7、文章会持续升级,鄙人这里也只是列出来了常用的方法,少量更深的更简洁的方法还需要点进去看源码,喜欢的话就关注下,谢谢。

结尾:
本文参考:
关于ReactiveObjC原理及流程简介https://www.songma.com/p/fecbe23d45c1
响应式编程之ReactiveObjC常见用法https://www.songma.com/p/6af75a449d90
【iOS 开发】ReactiveObjC(RAC)的使用汇总
https://www.songma.com/p/0845b1a07bfa

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » ReactiveObjC看这里就够了

发表回复