RAC简介
RAC就是一个第三方库,他可以大大简化你的代码过程。
官方的说,ReactiveCocoa(其简称为RAC)是由GitHub开源的一个应用于iOS和OS X开发的新框架。RAC具有函数式编程和响应式编程的特性。
为什么我们要学习RAC?
为了提高我们的开发效率。RAC在某些特定情况下开发时可以大大简化代码,并且目前来看安全可靠。
RAC高级用法--组合《iOS RAC - 组合》
我们来配置RAC
pod 'ReactiveObjC', '~> 3.0.0' //建议纯oc代码使用ReactiveObjC
下面介绍RAC的一些简单用法
按钮添加点击事件
[[btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
}];
KVO使用
//方法一
[_redView rac_observeKeyPath:@"frame" options:NSKeyValueObservingOptionNew observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {
NSLog(@"方法一 %@",value);
}];
//方法二
[[_redView rac_valuesForKeyPath:@"frame" observer:nil] subscribeNext:^(id _Nullable x) {
NSLog(@"方法二 %@",x);
}];
//方法三 宏写法
[RACObserve(_redView, frame) subscribeNext:^(id _Nullable x) {
NSLog(@"方法三%@",x);
}];
对textfield文字改变的监听
[[self.textFild rac_textSignal] subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
对Label imageView添加点击事件
UILabel *mylabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
[[tap rac_gestureSignal] subscribeNext:^(id x) {
NSLog(@"tap");
}];
[mylabel addGestureRecognizer:tap];
[self.view addSubview:mylabel];
通知
- (void)viewDidLoad {
[super viewDidLoad];
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"postData" object:nil] subscribeNext:^(NSNotification * _Nullable x) {
NSLog(@"%@",x);
}];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[[NSNotificationCenter defaultCenter] postNotificationName:@"postData" object:@{@"a":@"1223"}];
}
定时器使用
-(void)btnClick{
self.time = 10;
//这个就是RAC中的GCD
//每一秒执行一次,这里要加上释放信号,否则控制器推出后依旧会执行,看具体需求吧
@weakify(self)
//RACDisposable* disposable
self.disposable = [[RACSignal interval:1.0 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {
@strongify(self)
self.time --;
NSString *title =self.time>0?[NSString stringWithFormat:@"剩余%ld",self.time]:@"重新发送";
[self.btn setTitle:title forState:UIControlStateNormal];
if (self.time == 0) {
[self.disposable dispose];
}
}];
}
/*
创建一个RACDisposable,调用disposable方法等时候就会进入创建对象的block,把定时器释放掉
这两个宏就是为了解决循环引用的,且必须配套使用。
@weakify(self)
@strongify(self)
相当于下面:
__weak typeof(self) weakSelf = self;
__strong typeof(weakSelf) strongSelf = weakSelf;
*/
多个信号同时执行 rac_liftSelector
- 主要应用一个页面有多个请求,然后又要等到数据全部请求到,在刷新UI的时候
-(void)demo{
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id _Nonnull subscriber) {
[subscriber sendNext:@"111"];
return nil;
}];
RACSignal *signal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id _Nonnull subscriber) {
[subscriber sendNext:@"22222"];
return nil;
}];
[self rac_liftSelector:@selector(refeshUI::) withSignals:signal1,signal2, nil] ;
}
-(void)refeshUI:(id)one :(id)two{
NSLog(@"%@---%@",one,two);
}
多个订阅 RACMulticastConnection
- RACMulticastConnection其实是一个连接类,就是当一个信号被多次订阅,他可以帮我们避免多次调用创建信号中的block
RACSignal *signal=[RACSignal createSignal:^RACDisposable * _Nullable(id _Nonnull subscriber) {
[subscriber sendNext:@"123444"];
return nil;
}];
RACMulticastConnection *mut = [signal publish];
[mut.signal subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[mut.signal subscribeNext:^(id _Nullable x) {
NSLog(@"22%@",x);
}];
[mut connect];
绑定信号 bind
RACSubject *signal = [RACSubject subject];
[[signal bind:^RACSignalBindBlock _Nonnull{
return ^RACSignal *(id value, BOOL* stop){
NSLog(@"%@",value);
NSString *v = [NSString stringWithFormat:@"处理过的数据%@",value];
return [RACReturnSignal return:v];//需要导入头文件#import "RACReturnSignal.h"
};
}] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[signal sendNext:@"1111"];
映射
- 我们在使用map完成上面的功能
RACSubject *subject = [RACSubject subject];
[[subject map:^id _Nullable(id _Nullable value) {
return [NSString stringWithFormat:@"处理过的数据%@",value];
}] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
} ];
[subject sendNext:@"11"];
- 我们在使用flattenMap完成上面的功能
RACSubject*signalA = [RACSubject subject];
[[signalA flattenMap:^__kindof RACSignal * _Nullable(id _Nullable value) {
NSString *v = [NSString stringWithFormat:@"处理数据:%@",value];
//这里需要返回一个信号
return [RACReturnSignal return:v];
}]subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[signalA sendNext:@"123"];
- flattenMap的独特用法处理信号中的信号
RACSubject *subjectofsignal = [RACSubject subject];
RACSubject *subject1 = [RACSubject subject];
[[subjectofsignal flattenMap:^__kindof RACSignal * _Nullable(id _Nullable value) {
return value;
}]subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[subjectofsignal sendNext:subject1];
[subject1 sendNext:@"111"];
- flattenMap也可以处理双重订阅
- switchToLatest同样可以处理双重订阅
RACSubject *subjectofsignal = [RACSubject subject];
RACSubject *subject1 = [RACSubject subject];
[subjectofsignal.switchToLatest subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[subjectofsignal sendNext:subject1];
[subject1 sendNext:@"111"];
过滤 filter
[[self.username.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
return value.length>5;
}] subscribeNext:^(NSString * _Nullable x) {
NSLog(@"%@",x);
}];
忽略 ignore
RACSubject * subject = [RACSubject subject];
[[subject ignore:@"a"] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[subject sendNext:@"a"];
[subject sendNext:@"b"];
[subject sendNext:@"c"];
忽略相邻重复数据 distinctUntilChanged
RACSubject *subject = [RACSubject subject];
//忽略相邻重复数据
[[subject distinctUntilChanged]subscribeNext:^(id _Nullable x) {
}];
[subject sendNext:@"1"];
[subject sendNext:@"1"];
[subject sendNext:@"2"];
[subject sendNext:@"1"];
[subject sendNext:@"1"];