参考资料:
- ReactiveCocoa源码(OC版) https://github.com/ReactiveCocoa/ReactiveObjC
Swift, please see ReactiveCocoa or ReactiveSwift
常见用法
1:rac_signalForControlEvents监听事件,替换addTarget监听
[[self.myButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
@strongify(self);
//改变属性 触发KVO
self.userModel.userName=@"cnblogs";
//发送通知 触发通知监听
[[NSNotificationCenter defaultCenter] postNotificationName:MPTNotificationName object:nil];
NSLog(@"rac_signalForControlEvents监听事件 - ");
}];
```
2:rac_textSignal 监听文本框的文字改变
[[self.myTextField rac_textSignal] subscribeNext:^(id x) {
NSLog(@"rac_textSignal当前输入:%@",x);
}];
//distinctUntilChanged 相同就不会再响应 takeUntil:(RACSignal *):获取信号直到某个信号执行完成
[[[[self.myTextField rac_textSignal] distinctUntilChanged] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
NSLog(@"rac_textSignal-distinctUntilChanged当前输入:%@",x);
}];
```
3:rac_valuesAndChangesForKeyPath KVO监听
[[self.userModel rac_valuesAndChangesForKeyPath:@"userName"
options:NSKeyValueObservingOptionNew observer:nil] subscribeNext:^(id x) {
NSLog(@"userName now value:%@",x[0]);
}];
```
4:监听通知
[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:MPTNotificationName object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
NSLog(@"收到通知了");
}];
```
5:替换代理方法的实现 rac_signalForSelector
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"RAC" message:@"RAC TEST" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"other", nil];
[[self rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)] subscribeNext:^(RACTuple *tuple) {
NSLog(@"tuple.first:%@",tuple.first);
NSLog(@"tuple.second:%@",tuple.second);
NSLog(@"tuple.third:%@",tuple.third);
//tuple.first UIAlertView当前对象
//tuple.second索引值
}];
[alertView show];
// [[self rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:)] subscribeNext:^(id x) {
//
// }];
//UIAlertView 还有更简便的 上面只是为了演示rac_signalForSelector 下面的X就是索引值
//如果使用此方法rac_signalForSelector 不会再执行了。因为rac_buttonClickedSignal内部已经订阅了rac_signalForSelector方法
[[alertView rac_buttonClickedSignal] subscribeNext:^(id x) {
NSLog(@"index %@",x);
}];
```
6.处理多个请求,都返回结果的时候,统一做处理.
rac_liftSelector
RACSignal *request1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// 发送请求1
[subscriber sendNext:@"发送请求1"];
return nil;
}];
RACSignal *request2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// 发送请求2
[subscriber sendNext:@"发送请求2"];
return nil;
}];
// 使用注意:几个信号,参数一的方法就几个参数,每个参数对应信号发出的数据。
[self rac_liftSelector:@selector(updateUIWithR1:r2:) withSignalsFromArray:@[request1,request2]];
更新UI
- (void)updateUIWithR1:(id)data r2:(id)data1
{
NSLog(@"更新UI%@ %@",data,data1);
}
队列
//then用法
//用于连接两个信号,当第一个信号完成,才会连接then返回的信号
RACSignal *thenSignal=[aSignal then:^RACSignal *{
return bSignal;
}];
[thenSignal subscribeNext:^(id x) {
NSLog(@"thenSignal的值:%@",x);
}];
//输出 thenSignal的值:7 thenSignal的值:9 bSignal清理了 aSignal清理了
//then实例
[[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSLog(@"第一步");
[subscriber sendCompleted];//向订阅者发送完成信号,否者一直处于执行中
return nil;
}] then:^RACSignal *{
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSLog(@"第二步");
[subscriber sendCompleted];
return nil;
}];
}] then:^RACSignal *{
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSLog(@"第三步");
[subscriber sendCompleted];
return nil;
}];
}] subscribeCompleted:^{
NSLog(@"完成");
}];
//输出:第一步 第二步 第三步
//说明:then的用法要跟上面这样使用,它会在RACSignal里面就执行
- doNext doCompleted执行时间 doNext: 执行Next之前,会先执行这个Block doCompleted: 执行sendCompleted之前,会先执行这个Block
[[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"执行sendNext"];
NSLog(@"执行sendNext");
[subscriber sendCompleted];
return nil;
}] doNext:^(id x) {
NSLog(@"执行doNext");
}] doCompleted:^{
NSLog(@"执行doCompleted");
}] subscribeNext:^(id x) {
NSLog(@"执行subscribeNext");
}];
// 输出
// 执行doNext
// 执行subscribeNext
// 执行sendNext
// 执行doCompleted