ReactiveCocoa小结

1. 监听 KVO
  • 1.1> 监听对象的属性变化
[RACObserve(self.scrollView, contentSize) subscribeNext:^(id x) { 
}];
  • 1.2> 监听Bool值改变
[RACObserve(self, bCheck) subscribeNext:^(id x) { 
}];
  • 1.3> 监听方法
监听某个方法被调用会触发
[[self rac_signalForSelector:@selector(viewDidAppear:)] subscribeNext:^(id x) {
}];
可以指定某个代理中的方法
[[self rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)] subscribeNext:^(RACTuple *tuple) {
}];
监听UITextField变化
[textField.rac_textSignal subscribeNext:^(NSString *text) {
  //文本输入变化
}];
[[textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) {
  //文本输入变化
}]; 
[[textField rac_signalForControlEvents:UIControlEventEditingDidEnd] subscribeNext:^(id x) {
   //结束编辑
}];
RACObserve监听的对象属性返回值作为RAC监听对象属性的值
RAC(customBtn, hidden) = RACObserve(textField, hidden); 
等价于: 
[RACObserve(textField, hidden) subscribeNext:^(BOOL x) { 
customBtn.hidden = x; 
}]
2.事件
  • 2.1> 按钮点击事件
[[submitBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) { 
}];
  • 2.2> 手势事件
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init]; 
[[cancelTap rac_gestureSignal] subscribeNext:^(id x) { 
}];
3.通知
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"postData" object:nil] subscribeNext:^(NSNotification *notification) {
}];
4.替代代理 回调类似于block用法
信号创建
RACSubject * moreSignal = [RACSubject subject];
信号发送
[moreSignal sendNext:];
信号响应
[moreSignal subscribeNext:^(id x) {
}];
5.映射
map函数就是创建一个订阅者的映射并且返回数据,RAC监听对象属性的值,也就是customLabel.text根据textField的值来赋值,value的类型根据target监听属性值来定义
eg:textField的text是NSString类型. map函数需要返回值,类型必须和等号左边的RAC的接受值一致,如果返回BOOL则crash

RAC(customLabel, text) = [textField.rac_textSignal   map:^id(NSString *value) {
  return value;
}];
[[textFild.rac_textSignal map:^id(id value) {
       return @1;
}] subscribeNext:^(id x) {
       NSLog(@"%@", x);    //输出1,这个x是上面block中return返回值1
}];
6.过滤
  • 6.1> filter 可以帮助你筛选出你需要的值
[[self.textFild.rac_textSignal filter:^BOOL(NSString *value) {
     return [value length] > 3;
 }] subscribeNext:^(id x) {
     NSLog(@"x = %@", x);
}];
  • 6.2> ignore 可以忽略某些值
[[self.textFild.rac_textSignal filter:^BOOL(NSString *value) {
     return [value length] > 3;
 }] subscribeNext:^(id x) {
     NSLog(@"x = %@", x);
}];
  • 6.3> take 从开始一共取几次信号. 从头
RACSubject * subject = [RACSubject subject];
[[subject take:2] subscribeNext:^(id x) {
   NSLog(@"%@",x); // 1 2
}];
[subject sendNext:@"1"];
[subject sendNext:@"2"];
[subject sendNext:@"3"];
  • 6.4> takeLast 取后面的值 必须是发送完成
RACSubject * subject = [RACSubject subject];
[[subject takeLast:2] subscribeNext:^(id x) {
    NSLog(@"%@",x); // 2 3
}];
[subject sendNext:@"1"];
[subject sendNext:@"2"];
[subject sendNext:@"3"];
[subject sendCompleted];
  • 6.5> takeUntil 当传入的某个信号发送完成,这样就不会再接收源信号的内容,或者发送任意数据也不会再接收
RACSubject * subject = [RACSubject subject];
RACSubject * signal = [RACSubject subject];
[[subject takeUntil:signal] subscribeNext:^(id x) {
    NSLog(@"%@",x); //1 2
}];
[subject sendNext:@"1"];
[subject sendNext:@"2"];
[signal sendCompleted];
[subject sendNext:@"3"];
  • 6.6> distinctUntilChanged 如果当前的值跟上一个值相同,这样就不会被订阅发送信号
RACSubject * subject = [RACSubject subject];
[[subject distinctUntilChanged] subscribeNext:^(id x) {
    NSLog(@"%@",x); //A
}];
[subject sendNext:@"A"];
[subject sendNext:@"A"];
7.RACMulticastConnection

当我们多次订阅同一个信号的时候,避免订阅信号block中的代码被调用多次。

RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id subscriber) {
  return nil;
}];
RACMulticastConnection *connection = [signal publish];//转化为连接类
[connection.signal subscribeNext:^(id x) {
}];
[connection.signal subscribeNext:^(id x) {
}];
[connection connect]; 
8.rac_liftSelector:withSignalsFromArray:

当进入一个页面需要发多次请求,当全部请求结束再执行更新UI,可以使用下面RAC方法,可以替代多线程GCD的dispatch_group_enter和dispatch_group_leave 参数1:请求结束执行的方法,参数个数必须是和参数二的数组信号个数一致,是信号发送的值 参数2: 数组 存放所有信号

rac_liftSelector:withSignalsFromArray:
9.组合
  • 9.1> concat 数组组合
RACSequence *letters = [@"A B C D E F G H I" componentsSeparatedByString:@" "].rac_sequence;
RACSequence *numbers = [@"1 2 3 4 5 6 7 8 9" componentsSeparatedByString:@" "].rac_sequence;
RACSequence *concatenated = [letters concat:numbers];
[concatenated.signal subscribeNext:^(id x) {
    NSLog(@"%@",x); // Contains: A B C D E F G H I 1 2 3 4 5 6 7 8 9
}];
  • 9.2> merge 当多个信号执行同一种操作 使用merge
RACSubject *subject1 = [RACSubject subject];
RACSubject *subject2 = [RACSubject subject];
RACSignal *mergeSignal = [subject1 merge:subject2];
[mergeSignal subscribeNext:^(id x) {
    NSLog(@"%@",x);
}];
[subject1 sendNext:@"第一个位置调用"];
[subject1 sendNext:@"第二个位置调用"];
  • 9.3> zipWith 当希望两个信号都发出信号时才调用,并且会把两个信号的内容组成一个元组,和第8的作用非常一样
RACSubject *subject1 = [RACSubject subject];
RACSubject *subject2 = [RACSubject subject];
RACSignal *mergeSignal = [subject1 zipWith:subject2];
[mergeSignal subscribeNext:^(id x) {
    NSLog(@"%@",x);
}];
[subject1 sendNext:@"第一个位置调用"];
[subject1 sendNext:@"第二个位置调用"];
  • 9.4> combineLatest 将多个信号合并起来,当希望两个信号都发出信号时才调用,和9.3作用一样
RACSubject *subject1 = [RACSubject subject];
RACSubject *subject2 = [RACSubject subject];
RACSignal *mergeSignal = [RACSignal combineLatest:@[subject1,subject2] reduce:^id(NSString * title1,NSString * title2){
    NSLog(@"%@ -- %@",title1,title2);  //第一个位置调用 -- 第二个位置调用
    return @"返回值";
}];
[mergeSignal subscribeNext:^(id x) {
    NSLog(@"%@",x);   //返回值
}];
[subject1 sendNext:@"第一个位置调用"];
[subject2 sendNext:@"第二个位置调用"];
  • 9.5> reduce reduce是聚合的作用,讲多个信号分别发送的信号聚在一起返回。

你可能感兴趣的:(ReactiveCocoa小结)