参考:https://www.jianshu.com/p/5d891caa033d
一、基本使用
RACSubject *subject = [RACSubject subject];
[subject subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[subject subscribeNext:^(id _Nullable x) {
NSLog(@"------%@",x);
}];
[subject sendNext:@"111"];
/**1、 btn添加事件 */
[[_btn1 rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
NSLog(@"----subscribeNext-----%@",x);
}];
/**2、代替代理 监听指定方法调用,x为方法的参数 */
[[self.hkView rac_signalForSelector:@selector(handleClickBtnWithStr:)] subscribeNext:^(RACTuple * _Nullable x) {
NSLog(@"----subscribeNext-----%@",x);
}];
/**3、 KVO */
[self.hkView rac_observeKeyPath:@"frame" options:(NSKeyValueObservingOptionNew) observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {
NSLog(@"-------%@",value);
}];
/**4、代替kvo,监听属性变化 为frame设置为信号量,并订阅它 */
[[self.hkView rac_valuesForKeyPath:@"frame" observer:nil] subscribeNext:^(id _Nullable x) {
NSLog(@"--__%@",x);
}];
[RACObserve(self.hkView, frame) subscribeNext:^(id _Nullable x) {
NSLog(@"----RACObserve-----%@",x);
}];
/**5、 通知 */
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
NSLog(@"---------%@",x);
}];
/**6、 textField */
// 监听textField的text变化
[_textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
NSLog(@"-----rac_textSignal----%@",x);
}];
// 监听的textfield control事件
[[_textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
NSLog(@"---------%@",x);
}];
// 接受满足一定条件的text改变事件
[[_textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
return value.length > 3;
}] subscribeNext:^(NSString * _Nullable x) {
NSLog(@"---------%@",x);
}];
/*7、 NSTimer */
NSThread *subThread = [[NSThread alloc] initWithBlock:^{
NSTimer *timer1 = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
NSLog(@"---------%@",@"111");
}];
[[NSRunLoop currentRunLoop] addTimer:timer1 forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSince1970:1]];
}];
[subThread start];
// RAC应用timer
[[RACSignal interval:1 onScheduler:[RACScheduler scheduler]] subscribeNext:^(NSDate * _Nullable x) {
NSLog(@"----interval-----%@",x);
}];
// 添加延迟
[[[RACSignal interval:1 onScheduler:[RACScheduler scheduler]] delay:2] subscribeNext:^(NSDate * _Nullable x) {
NSLog(@"---------%@",@"dddd");
}];
/**8、 遍历数组 */
NSArray *array = @[@"1", @"3"];
[array.rac_sequence.signal subscribeNext:^(id _Nullable x) {
NSLog(@"---------%@",x);
}];
二、常用的类
1、RACSignal: 信号
RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext:@"aadf"];
return [RACDisposable disposableWithBlock:^{
NSLog(@"---------%@",@"dealloc");
}];
}];
[signal subscribeNext:^(id _Nullable x) {
NSLog(@"---------%@",x);
}];
2、RACSubject:(可发送信号也可以订阅信号)
RACReplaySubject 可接受订阅前的事件
RACSubject *subject = [RACSubject subject];
[subject subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[subject subscribeNext:^(id _Nullable x) {
NSLog(@"------%@",x);
}];
[subject sendNext:@"111"];
RACReplaySubject *replaySubjec = [RACReplaySubject subject];
[subject subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[subject sendNext:@"111"];
[subject subscribeNext:^(id _Nullable x) {
NSLog(@"------%@",x);
}];
3、 RACTuple(元组): 其内部就是封装了数组,用起来跟数组差不多
RACTuple *tuple = [RACTuple tupleWithObjects:@"1", @"3", nil];
RACTuple *tuple2 = RACTuplePack(@"1", @"2");
NSLog(@"---------%@",tuple.first);
4、 RACMulticastConnection: 用于当一个信号,被多次订阅时,避免多次调用创建信号中的disposableWithBlock
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext:@"hhhha"];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"---------%@",@"xiaoguole");
}];
}];
RACMulticastConnection *connection = [signal1 publish];
[connection.signal subscribeNext:^(id _Nullable x) {
NSLog(@"---1------%@",x);
}];
[connection.signal subscribeNext:^(id _Nullable x) {
NSLog(@"---2------%@",x);
}];
[connection connect];
5、 RACCommond 监听信号的状态等
NSString *inputStr = @"执行";
RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext:@"ahahaha"];
[subscriber sendNext:@"lalaal"];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"---------%@",@"销毁了");
}];
}];
}];
[command.executionSignals subscribeNext:^(RACSignal * _Nullable x) {
NSLog(@"----executionSignals--11---%@",x);
[x subscribeNext:^(id _Nullable x) {
NSLog(@"-----executionSignals--22--%@",x);
}];
}];
// 当订阅了信号的信号,切换到最新的信号
[[command.executionSignals switchToLatest] subscribeNext:^(id _Nullable x) {
NSLog(@"-----switchToLatest----%@",x);
}];
// x为1 正在执行、0已经最新完毕
[[command.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
NSLog(@"----executing-----%@",x);
}];
// 错误进入
[command.errors subscribeNext:^(NSError * _Nullable x) {
NSLog(@"----errors-----%@",x);
}];
[command execute:inputStr];
三:高级用法
1、concat、2、combineLatestWith;3、then;4、merge;5:zipWith;6:combineLatest: reduce;7:map,flattenMap;8:filter,ignore,distinctUntilChanged,takeUntil
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext:@"1111"];
[subscriber sendNext:@"3333"];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"---------%@",@"销毁了111");
}];
}];
RACSignal *signal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext:@"444"];
[subscriber sendNext:@"2222"];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"---------%@",@"销毁了222");
}];
}];
// concat: 链接两个信号,按顺序输出
RACSignal *signal3 = [signal1 concat:signal2];
[signal3 subscribeNext:^(id _Nullable x) {
NSLog(@"-----signal3----%@",x);
}];
// combineLatestWith: 合并两个信号,获取signal1信号的最新发送值,必须每个信号都发送值才能订阅成功
RACSignal *signal4 = [signal1 combineLatestWith:signal2];
[signal4 subscribeNext:^(id _Nullable x) {
NSLog(@"----signal4-----%@",x);
}];
// signal1 执行完毕了再执行then返回的signale2,signal5 = signal2
// RACSignal *signal5 = [signal1 then:^RACSignal * _Nonnull{
// return signal2;
// }];
// [signal5 subscribeNext:^(id _Nullable x) {
// NSLog(@"-----signal5----%@",x);
// }];
/* 打印结果:
2021-10-21 15:29:27.770515+0800 RACProject[4188:250216] -----signal5----2222
2021-10-21 15:29:27.770605+0800 RACProject[4188:250216] ---------销毁了222
2021-10-21 15:29:27.770679+0800 RACProject[4188:250216] ---------销毁了111
*/
// merge:合并两个信号,任何一个信号的值有改变就会通知接口方法
// RACSignal *signal6 = [signal1 merge:signal2];
// [signal6 subscribeNext:^(id _Nullable x) {
// NSLog(@"----signal6-----%@",x);
// }];
/* 打印:
RACProject[2817:40066] ----signal6-----1111
2021-10-28 09:19:50.063668+0800 RACProject[2817:40066] ----signal6-----3333
2021-10-28 09:19:50.063765+0800 RACProject[2817:40066] ---------销毁了111
2021-10-28 09:19:50.063860+0800 RACProject[2817:40066] ----signal6-----444
2021-10-28 09:19:50.063942+0800 RACProject[2817:40066] ----signal6-----2222
2021-10-28 09:19:50.064039+0800 RACProject[2817:40066] ---------销毁了222
**/
//一个信号signal7去监听signal1和signal2,但必须两个信号都有发出(不需要同时,例如signal1信号发出了,signal2信号等了10秒之后发出,那么signal7的订阅回调是等signal2信号发出的那一刻触发),成对监听的
// RACSignal *signal7 = [signal1 zipWith:signal2];
// [signal7 subscribeNext:^(id _Nullable x) {
// NSLog(@"-----signal7----%@",x);
// }];
/* 打印结果
2021-10-21 15:39:35.280033+0800 RACProject[4332:264115] ---------销毁了111
2021-10-21 15:39:35.280308+0800 RACProject[4332:264115] -----signal7----<RACTwoTuple: 0x600000670180> (
1111,
444
)
2021-10-21 15:39:35.280433+0800 RACProject[4332:264115] -----signal7----<RACTwoTuple: 0x60000066c380> (
3333,
2222
)
2021-10-21 15:39:35.280563+0800 RACProject[4332:264115] ---------销毁了222
*/
//combineLatest: reduce: :处理接收到的signal1和signal2的值,将处理后的新值发出去
// RACSignal *signal8 = [RACSignal combineLatest:@[signal1, signal2] reduce:^id _Nonnull(NSString *s1, NSString *s2) {
// return [s1 stringByAppendingString:s2];
// }];
// [signal8 subscribeNext:^(id _Nullable x) {
// NSLog(@"---------%@",x);
// }];
/*打印结果:
2021-10-21 15:48:51.860925+0800 RACProject[4405:272117] ---------销毁了111
2021-10-21 15:48:51.861115+0800 RACProject[4405:272117] ---------3333444
2021-10-21 15:48:51.861289+0800 RACProject[4405:272117] ---------33332222
2021-10-21 15:48:51.861413+0800 RACProject[4405:272117] ---------销毁了222
*/
// map, flattenMap 映射:处理接收到的值
// [[_textField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) {
// return [value stringByAppendingString:@"aaaa"];
// }] subscribeNext:^(id _Nullable x) {
// NSLog(@"---------%@",x);
// }];
// [[_textField.rac_textSignal flattenMap:^__kindof RACSignal * _Nullable(NSString * _Nullable value) {
// return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
// [subscriber sendNext:[value stringByAppendingString:@"0000"]];
// [subscriber sendCompleted];
// return [RACDisposable disposableWithBlock:^{
// NSLog(@"---------%@",@"销毁掉");
// }];
// }];
// }] subscribeNext:^(id _Nullable x) {
// NSLog(@"---------%@",x);
// }];
// filter: 过滤不符合的条件,ignore:忽略掉值, distinctUntilChanged: 过滤掉相同的值
[[signal1 filter:^BOOL(NSString * _Nullable value) {
return [value isEqual:@"1111"];
}] subscribeNext:^(id _Nullable x) {
NSLog(@"-----filter----%@",x);
}];
[[signal1 ignore:@"1111"] subscribeNext:^(id _Nullable x) {
NSLog(@"----ignore-----%@",x);
}];
RACSubject *subject1 = [RACSubject subject];
[[subject1 distinctUntilChanged] subscribeNext:^(id _Nullable x) {
NSLog(@"----distinctUntilChanged-----%@",x);
}];
[subject1 sendNext:@"1"];
[subject1 sendNext:@"1"];
[subject1 sendNext:@"2"];
[subject1 sendNext:@"2"];
[subject1 sendNext:@"5"];
[subject1 sendCompleted];
// takeUntil:在收到signal3通知后释放前面的信号signal1, signal2
[[[RACSignal combineLatest:@[signal1, signal2]] takeUntil:signal3] subscribeNext:^(RACTuple * _Nullable x) {
NSLog(@"------signaleCombine---%@",x);
}] ;