IOS RAC-初级用法

作者: wp_Demo | 来源:发表于2019-01-31 11:05 被阅读0次
    1. 创建信号
        //1:创建信号
        RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
           
            //3:发送信号
            [subscriber sendNext:@"wp"];
            
            //请求网络 失败 error
            NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:10086 userInfo:@{@"key":@"10086错误"}];
            
            [subscriber sendError:error];
            
            //RACDisposable 销毁 
            return [RACDisposable disposableWithBlock:^{
                NSLog(@"销毁了");
            }];
        }];
        
        
        //2:订阅信号
        [signal subscribeNext:^(id  _Nullable x) { 
            NSLog(@"subscribeNext %@",x);
        }];
        
        //订阅错误信号
        [signal subscribeError:^(NSError * _Nullable error) {
            NSLog(@"subscribeError %@",error);
        }];
    

    打印如下:

    subscribeNext wp
    销毁了
    subscribeError Error Domain=NSURLErrorDomain Code=10086 "(null)" UserInfo={key=10086错误}
    销毁了

    2. 替代KVO

    监听name的变化

        [RACObserve(self, name) subscribeNext:^(id  _Nullable x) {
            
            NSLog(@"%@",x);
            
        }];
    
    3. 替代代理

    设置代理,然后进行监听

        [[self rac_signalForSelector:@selector(textFieldDidBeginEditing:) fromProtocol:@protocol(UITextFieldDelegate)] subscribeNext:^(RACTuple * _Nullable x) {
            
            NSLog(@"textFieldDidBeginEditing %@",x);
        }];
        
        self.textField.delegate = self;
    
    4. 替代通知
        [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillShowNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
           
            NSLog(@"%@",x);
        }];
    
    5. 替代UI逻辑
        //button 点击
        [[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            NSLog(@"%@",x);
        }];
    
        //textField 文字监听
        [self.textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
    
            NSLog(@"%@",x);
        }];
    
        //手势监听
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        self.label.userInteractionEnabled = YES;
        [self.label addGestureRecognizer:tap];
        [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            NSLog(@"%@",x);
        }];
    
    
    6. 替代数据遍历
    //数组
    NSArray *array = @[@"Cooci",@"123",@"18"];
    
    [array.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
       
        NSLog(@"%@",x);
    }];
    
    NSDictionary *dict = @{@"key":@"Cooci",@"age":@"18",@"gender":@"1"};
    
    [dict.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
       //类似swift中的元祖
        NSLog(@"%@",x);
        
        RACTwoTuple *tuple = (RACTwoTuple *)x;
        
        NSLog(@"key == %@ , value = %@",tuple[0],tuple[1]);
      
    }];
    

    2019-01-31 10:54:33.728300+0800 001---RAC初探[2254:133490] wp
    2019-01-31 10:54:33.728657+0800 001---RAC初探[2254:133490] 123
    2019-01-31 10:54:33.728871+0800 001---RAC初探[2254:133490] 18
    2019-01-31 10:54:33.729287+0800 001---RAC初探[2254:133489] <RACTwoTuple: 0x600002004390> (
    key,
    wp
    )
    2019-01-31 10:54:33.729404+0800 001---RAC初探[2254:133489] key == key , value = wp
    2019-01-31 10:54:33.729639+0800 001---RAC初探[2254:133489] <RACTwoTuple: 0x6000020109a0> (
    age,
    18
    )
    2019-01-31 10:54:33.729744+0800 001---RAC初探[2254:133489] key == age , value = 18
    2019-01-31 10:54:33.729933+0800 001---RAC初探[2254:133489] <RACTwoTuple: 0x6000020109e0> (
    gender,
    1
    )
    2019-01-31 10:54:33.730042+0800 001---RAC初探[2254:133489] key == gender , value = 1

    7. 替代Timer
        NSLog(@"执行前 %@",[NSThread currentThread]);
        [[RACSignal interval:1 onScheduler:[RACScheduler schedulerWithPriority:(RACSchedulerPriorityHigh) name:@"com.ReactiveCocoa.RACScheduler.mainThreadScheduler"]] subscribeNext:^(NSDate * _Nullable x) {
            NSLog(@"执行时 %@",[NSThread currentThread]);
        }];
    

    可以发现timer是在子线程执行的

    2019-01-31 10:56:19.174662+0800 001---RAC初探[2346:137587] 执行前 <NSThread: 0x60000124e900>{number = 1, name = main}
    2019-01-31 10:56:20.178857+0800 001---RAC初探[2346:137617] 执行时 <NSThread: 0x600001229d80>{number = 3, name = (null)}
    2019-01-31 10:56:21.177887+0800 001---RAC初探[2346:137617] 执行时 <NSThread: 0x600001229d80>{number = 3, name = (null)}
    2019-01-31 10:56:22.178262+0800 001---RAC初探[2346:137617] 执行时 <NSThread: 0x600001229d80>{number = 3, name = (null)}
    2019-01-31 10:56:23.177743+0800 001---RAC初探[2346:137617] 执行时 <NSThread: 0x600001229d80>{number = 3, name = (null)}

    相关文章

      网友评论

        本文标题:IOS RAC-初级用法

        本文链接:https://www.haomeiwen.com/subject/rzlksqtx.html