RAC---iOS笔记摘录

目录

RAC(ReactiveCocoa) 
    函数式编程、响应式编程    
    Github上的开源框架---提高开发效率


引入
    pod 'ReactiveObjC'
    #import 
RAC---iOS笔记摘录_第1张图片
RAC框架图
RAC---iOS笔记摘录_第2张图片
  1. 常用
添加监听事件
    // 给UITextFiled添加值改变事件
    [[[UITextField new]rac_signalForControlEvents:UIControlEventEditingChanged]subscribeNext:^(id x) {
        //
    }];
    等同于(简化版)
    [[[UITextField new]rac_textSignal]subscribeNext:^(NSString * _Nullable x) {
        // UIControlEventEditingChanged
    }];

    // 给按钮添加点击事件
    [[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        NSLog(@"%@",x);
    }];
添加手势
    [[UIButton new]addGestureRecognizer:({
        UITapGestureRecognizer *tapG=[UITapGestureRecognizer new];
        [[tapG rac_gestureSignal]subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        }];
        tapG;
    })];
调用方法
    @weakify(self)
    [[self rac_signalForSelector:@selector(viewDidLoad)]subscribeNext:^(id x) {
        @strongify(self)
        //
    } error:^(NSError * _Nullable error) {
    } completed:^{
    }];
dele代理
    UIAlertView *alertV=[[UIAlertView alloc]initWithTitle:@"alert" message:@"content" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"sure", nil];
    // 实现代理方法clickedButtonAtIndex 来自协议UIAlertViewDelegate
    [[self rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)]subscribeNext:^(id  _Nullable x) {
    }];
    // 等同于(简化版)
    [[alertV rac_buttonClickedSignal]subscribeNext:^(NSNumber * _Nullable x) {
    }];
    [alertV show];
NSNotificationCenter通知
    添加观察者
    [[[NSNotificationCenter defaultCenter]rac_addObserverForName:@"notiName" object:nil]subscribeNext:^(NSNotification * _Nullable noti) {
        // noti.name noti.object noti.object
    }];
    发送通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"notiName" object:@[@"1",@"2"]];
KVO键值观察
    // 方式一
    [RACObserve([UIScrollView new], contentOffset) subscribeNext:^(id  _Nullable x) {
    }];
    // 方式二
    [[self rac_valueForKeyPath:@"age" observer:nil] subscribeNext:^(id x ){
        NSLog(@"%@",x);
    }];
RACSubject
    // 用于view中不同button的点击事件
    RACSubject *subj=[RACSubject subject];
    [subj sendNext:@(100)];
    // 外调
    [subj subscribeNext:^(NSNumber *tag) {
    }];
    
cell.clickSubject 
    @weakify(self);
    [[cell.clickSubject takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(NSNumber *x) {
        @strongify(self);
        [self clickCellBtn:[x intValue] atIndex:indexPath];
    }];
RAC(self.label.text) = _textField.rac_textSignal;
RAC(_personVM,mobile)=_phoneTF.rac_textSignal;
  1. 接口命令(用于MVVM)
+(RACSignal *)PostWithURL:(NSString *)urlStr parameters:(NSDictionary *)paramDic animated:(BOOL)animated{
    return [[RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        
            [YTNetworkTool POST:urlStr parameters:paramDic SuccessBlock:^(BOOL isOK, NSDictionary *dic) {
                // 回调
                [subscriber sendNext:dic];
                [subscriber sendCompleted];
            } failBlock:^(NSError *error) {
                [subscriber sendError:error];
            } animated:animated];
    
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"信号发送完成或发送错误后调用,会自动执行这个block,并取消订阅信号");
        }];
    }]replayLazily];
}
    _addRouteCommand=[[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        //
        NSDictionary *paramDic=@{@"isOpen":self.isOpen};
        return [YTServiceObject PostWithURL:API_AddRouteT parameters:paramDic animated:true];
    }];
    执行命令(调用接口)
    vm.isOpen=@(true);
    [[vm.addRouteCommand execute:nil] subscribeNext:^(NSDictionary *dic) { 
      // dic
    }];
    [[[vm.addRouteCommand execute:nil]deliverOnMainThread] subscribeNext:^(NSDictionary *dic) { 
      // dic
    }];

    // 命令在执行中(用于加载 加载中...)
    [[vm.addRouteCommand executing]subscribeNext:^(NSNumber * _Nullable x) {
        if(x.boolValue){
            // 加载 加载中...
        }else{
            // 隐藏 加载中...
        }
    }];

两个信号都收到时回调

RACSignal*signalA = [RACSignal createSignal:^RACDisposable *(id subscriber){
  NSLog(@"数据请求1");
[subscriber sendNext:@"数据请求1请求下来的数据"];
return nil;
}];
RACSignal*signalB = [RACSignal createSignal:^RACDisposable *(id subscriber){
  NSLog(@"数据请求2");
[subscriber sendNext:@"数据请求2请求下来的数据"];
return nil;
}];
[self rac_liftSelector:@Selector(updateUI) withSignalFromArray:@[signalA,signalB]];
  1. 信号加条件
map 映射(拦截做额外处理)

    [[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside]map:^id(id value) {
        NSLog(@"value: %@",value);
        return @"hello";
    }]subscribeNext:^(id x) {
        NSLog(@"%@",x);
    }];
filter 过滤(拦截不符合情况)

    [[self.textField.rac_textSignal filter:^BOOL(id value) {
        return [value length] > 3;
    }]subscribeNext:^(id x) {
        NSLog(@"x:%@",x);
    }];
take:2   选取前两个信号
skip:2   跳过前两个信号
repeat   无限重复执行
delay:3  延迟3s发送信号
throttle:0.5           0.5s内信号不发生变化则触发
distinctUntilChanged   不会连续发送两次相同的信号
timeout:2       超时2s后触发error
ignore:@"1"     忽略信号1


[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.textField endEditing:YES];
        //        [alertView show];
      
        // 创建信号
        RACSignal *signal = [[RACSignal createSignal:^RACDisposable *(id subscriber) {
            [subscriber sendNext:@"1"];
            [subscriber sendNext:@"2"];
            [subscriber sendNext:@"3"];
            [subscriber sendNext:@"4"];
            [subscriber sendCompleted];
            return nil;
        }] take:2];  // skip:2、repeat
        
        // 发送信号
        [signal subscribeNext:^(id x) {
            NSLog(@"x : %@",x);
        } completed:^{
            NSLog(@"completed");
        }];
    }];
  1. 基础
信号RACSignal
    // 创建一个信号
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id subscriber) {
        [subscriber sendNext:@"唱歌"];
        [subscriber sendCompleted];
        return nil;
    }];

你可能感兴趣的:(RAC---iOS笔记摘录)