这篇文章给大家分享的是有关怎么使用ReactiveObjC的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。概述RAC架构框架图信号流程基本使用1、基本控件UITextField//监听文本输入 [[_te
这篇文章给大家分享的是有关怎么使用ReactiveObjC的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
信号流程
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);}];
//监听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;
//遍历数组 NSArray *array = @[@"1", @"2", @"3", @"4", @"5"];[array.rac_sequence.signal subscribeNext:^(id _Nullable x) { NSLog(@"内容-->%@", x)<br>}];
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"notification" object:nil] subscribeNext:^(NSNotification * _Nullable x) { NSLog(@"-->%@", x);}];
//监听按钮点击方法的信号//当执行完btnClickAction后会执行此订阅[[self rac_signalForSelector:@selector(btnClickAction:)] subscribeNext:^(RACTuple * _Nullable x) { NSLog(@"-->%@", x);}];-(void) btnClickAction:(UIButton *)btn{ NSLog(@"按钮点击");}
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(元组)-- 其内部就是封装了数组,用起来跟数组差不多
//通过定值创建RACTupleRACTuple *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];
首先创建两个信号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、 concat -- 当多个信号发出的时候,有顺序的接收信号
一个信号signal3去监听signal1和signal2,必须是接收signal1完后才会接收signal2
RACSignal *signal3 = [signal1 concat:signal2]; [signal3 subscribeNext:^(id _Nullable x) { NSLog(@"signal3-->%@",x); }];
2、 combineLatestWith -- 将多个信号合并起来,并且拿到各个信号的最新的值,必须每个合并的signal至少都有过一次sendNext,才会触发合并的信号(订阅者每次接收的参数都是所有信号的最新值),不论触发哪个信号都会触发合并的信号
一个信号signal3去监听signal1和signal2,每次回调两个信号的最新值
RACSignal *signal3 = [signal1 combineLatestWith:signal2]; [signal3 subscribeNext:^(id _Nullable x) { NSLog(@"%@",x); }];
PS:注释signal1的sendNext方法,直接销毁了,所以每个singal必须有sendNext方法
3、 then -- 用于连接两个信号,等待第一个信号完成,才会连接then返回的信号
RACSignal *signal3 = [signal1 then:^RACSignal * _Nonnull{ return signal2;}];[signal3 subscribeNext:^(id _Nullable x) { NSLog(@"signal3-->%@",x);}];
4、 merge -- 把多个信号合并为一个信号来监听,任何一个信号有新值的时候就会调用
一个信号signal3去监听signal1和signal2,每次回调一个信号
RACSignal *signal3 = [signal1 merge:signal2];[signal3 subscribeNext:^(id _Nullable x) { NSLog(@"signal3-->%@",x);}];
5、 zipWith -- 把两个信号压缩成一个信号,只有当两个信号都发出信号内容时,才会触发
一个信号signal3去监听signal1和signal2,但必须两个信号都有发出(不需要同时,例如signal1信号发出了,signal2信号等了10秒之后发出,那么signal3的订阅回调是等signal2信号发出的那一刻触发)
RACSignal *signal3 = [signal1 zipWith:signal2];[signal3 subscribeNext:^(id _Nullable x) { NSLog(@"signal3-->%@",x);}];
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);}];
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); }] ;
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”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
--结束END--
本文标题: 怎么使用ReactiveObjC
本文链接: https://lsjlt.com/news/279399.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0