RAC基本用法

2018-01-02  本文已影响39人  CoderLWG

1.信号的创建、订阅和发送

1.1创建信号

RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        NSLog(@"Here I am!");
        // 发送信号,在创建信号完成的Block中进行
        [subscriber sendNext:@"This is RAC"];
        return nil;
    }];

1.2 订阅信号

// 订阅信号,这里如果不进行订阅,上面的"Here I am!"也不会执行,订阅到的信号内容就是上面发送的"This is RAC"
    [signal subscribeNext:^(id  _Nullable x) {
        // 信号的内容
        NSLog(@"%@",x);
    }];

1.3 合并到一块的写法

[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        NSLog(@"Here I am too!");
        // 发送信号
        [subscriber sendNext:@"This is RAC too"];
        return nil;
    }] subscribeNext:^(id  _Nullable x) {
        // 信号的内容
        NSLog(@"%@",x);
    }];

2.RAC信号的监听

2.1通知监听

 [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        NSLog(@"%@",x);
    }];

当程序进入后台时,打印
NSConcreteNotification 0x604000259620 {name = UIApplicationDidEnterBackgroundNotification; object = <UIApplication: 0x7f8e546022a0>}

2.2 KVO监听

监听person对象的name属性的改变

    self.person = [[Person alloc] init];
    self.person.name = @"haha";
    [RACObserve(_person, name) subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];

2.3 监听textField输入

textField输入内容的改变都会通过x打印出来

 [[self.textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"%@",x);
    }];

3. RAC中循环引用

3.1 OC中传统解决循环引用的方法

/*
 OC 传统方法解决循环引用
block 外面  __weak typeof(self) weakSelf = self;
block 里面 __strong typeof(weakSelf) strongSelf = weakSelf;
使用 __unsafe_unretained 來修飾 weakSelf, 如果 self 被釋放的話 那麼 weakSelf 依然會記錄原來的地址 (野指針)
 */
    __weak typeof(self) weakSelf = self;
    [[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        NSLog(@"%@",x);
        // 为了保证block 再传出去  VC 之后被销毁,block仍能正常运行
        // strongSelf 是对weakSelf进行了强引用
        __strong typeof(weakSelf) strongSelf = weakSelf;
        strongSelf.nameLabel.text = @"Hello";
        
    }];

3.2 RAC中解决循环引用的方法

// @weakify(self); 为了打断循环引用,在外面使用@weakify(self)
// @strongify(self); 在block里面使用@strongify(self)
   @weakify(self)
   [[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
       NSLog(@"%@",x);
       
       @strongify(self)
       self.nameLabel.text = @"Hello";
   }];

4. 联合监听多个控件

// 使用原始方法需要一个一个去监听,使用combineLatest直接把监听对象放进数组中,返回一个元组对象
 UITextField *nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 400, 300, 40)];
    nameTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:nameTextField];
    
    UITextField *pwdTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 440, 300, 40)];
    pwdTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:pwdTextField];
    
//    [[nameTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
//       NSLog(@"%@",x);
//    }];
//    [[pwdTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
//        NSLog(@"%@",x);
//    }];
    
    //    combineLatest 把多個信號 捆綁成一個信號 最多不要 超過 五個
    //    RACTuple 元组 它可以像是一個字典,裡面可以放多種類型
    [[RACSignal combineLatest:@[nameTextField.rac_textSignal, pwdTextField.rac_textSignal]] subscribeNext:^(RACTuple * _Nullable x) {
       // NSLog(@"%@,%@",x,[x class]);
        
        NSString * name = x.first;
        NSString * pwd = x.second;
        NSLog(@"%@ ,%@", name,pwd);
    }];


     //    id 是一个返回值
     //    reduce  合并信号的数据,进行汇总计算使用
     //    判断用户名和密码同时存在的时候,按钮才能点击!
    [[RACSignal combineLatest:@[nameTextField.rac_textSignal, pwdTextField.rac_textSignal] reduce:^id _Nullable(NSString *name, NSString *pwd){
        NSLog(@"%@ ,%@", name,pwd);
        return @(name.length > 0 && pwd.length > 0);
        
        return nil;
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
        _button.enabled = [x boolValue];
    }];

5. 快速遍历数组和字典

5.1 遍历数组

 NSArray *arr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7"];
    // 在数组中遍历元素
    for (id obj in arr) {
        NSLog(@"%@,%@",obj,[NSThread currentThread]);
    }
打印结果:可以看出都是在主线程执行
2018-01-04 16:26:20.092313+0800 MKJMWWM[2703:289030] 1,<NSThread: 0x6000000700c0>{number = 1, name = main}
2018-01-04 16:26:20.092467+0800 MKJMWWM[2703:289030] 2,<NSThread: 0x6000000700c0>{number = 1, name = main}
2018-01-04 16:26:20.092559+0800 MKJMWWM[2703:289030] 3,<NSThread: 0x6000000700c0>{number = 1, name = main}
2018-01-04 16:26:20.092650+0800 MKJMWWM[2703:289030] 4,<NSThread: 0x6000000700c0>{number = 1, name = main}
2018-01-04 16:26:20.092751+0800 MKJMWWM[2703:289030] 5,<NSThread: 0x6000000700c0>{number = 1, name = main}
2018-01-04 16:26:20.092842+0800 MKJMWWM[2703:289030] 6,<NSThread: 0x6000000700c0>{number = 1, name = main}
2018-01-04 16:26:20.092946+0800 MKJMWWM[2703:289030] 7,<NSThread: 0x6000000700c0>{number = 1, name = main}

5.2 数组转换成rac_sequence在进行遍历

    [arr.rac_sequence.signal subscribeNext:^(id x) {
       NSLog(@"%@,%@",x,[NSThread currentThread]);
    } error:^(NSError *error) {
        NSLog(@"%@",error);
    } completed:^{
        NSLog(@"遍历完成了,%@",[NSThread currentThread]);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSDictionary *dict = @{@"key1":@"111",@"key2":@"222",@"key3":@"333",@"key4":@[@"M",@"K",@"J"]};
           
            [dict.rac_sequence.signal subscribeNext:^(id x) {
//                 RACTupleUnpack宏:专门用来解析元组
//                 RACTupleUnpack 等式右边:需要解析的元组
//                 宏的参数,填解析的什么样数据 元组里面有几个值,宏的参数就必须填几个
                RACTupleUnpack(id key,id value) = x;
                NSLog(@"%@,%@,%@",key,value,[NSThread currentThread]);
              
            } completed:^{
                NSLog(@"字典解析完成");
            }];
        });
    }];
打印内容如下:可以看出打印内容不再主线程
2018-01-04 16:39:31.657545+0800 MKJMWWM[2826:338427] 1,<NSThread: 0x604000279cc0>{number = 3, name = (null)}
2018-01-04 16:39:31.657865+0800 MKJMWWM[2826:338427] 2,<NSThread: 0x604000279cc0>{number = 3, name = (null)}
2018-01-04 16:39:31.658063+0800 MKJMWWM[2826:338427] 3,<NSThread: 0x604000279cc0>{number = 3, name = (null)}
2018-01-04 16:39:31.658219+0800 MKJMWWM[2826:338427] 4,<NSThread: 0x604000279cc0>{number = 3, name = (null)}
2018-01-04 16:39:31.658470+0800 MKJMWWM[2826:338427] 5,<NSThread: 0x604000279cc0>{number = 3, name = (null)}
2018-01-04 16:39:31.658636+0800 MKJMWWM[2826:338427] 6,<NSThread: 0x604000279cc0>{number = 3, name = (null)}
2018-01-04 16:39:31.658778+0800 MKJMWWM[2826:338427] 7,<NSThread: 0x604000279cc0>{number = 3, name = (null)}
2018-01-04 16:39:31.659790+0800 MKJMWWM[2826:338427] 遍历完成了,<NSThread: 0x604000279cc0>{number = 3, name = (null)}
2018-01-04 16:39:31.697010+0800 MKJMWWM[2826:338426] key3,333,<NSThread: 0x604000279240>{number = 4, name = (null)}
2018-01-04 16:39:31.697445+0800 MKJMWWM[2826:338426] key1,111,<NSThread: 0x604000279240>{number = 4, name = (null)}
2018-01-04 16:39:31.697844+0800 MKJMWWM[2826:338426] key4,(
    M,
    K,
    J
),<NSThread: 0x604000279240>{number = 4, name = (null)}
2018-01-04 16:39:31.698190+0800 MKJMWWM[2826:338426] key2,222,<NSThread: 0x604000279240>{number = 4, name = (null)}
2018-01-04 16:39:31.698416+0800 MKJMWWM[2826:338426] 字典解析完成
RAC汇总表.png
上一篇 下一篇

猜你喜欢

热点阅读