ReactiveCocoa基础篇

2018-03-29  本文已影响16人  马克吐温Coder

ReactiveCocoa简介

ReactiveCocoa几大消息类

1.RACSignal

    /**<1.创建信号*/
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
       /**<3.发送信号*/
        [subscriber sendNext:@"我跑了"];
        /**<4.取消订阅信号*/
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"我取消了订阅");
        }];
    }];
    /**<2.订阅信号*/
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];

2.RACSubject(RACSignal子类)

    /**<1.创建信号*/
    RACSubject *subject = [RACSubject subject];
    /**<2.订阅信号*/
    [subject subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];
    /**<3.发送信号*/
    [subject sendNext:@"我也跑了"];

3.RACReplaySubject(RACSubject子类,可以先发送信号,在订阅信号)

    /**<1.创建信号*/
    RACReplaySubject *replaySubjct = [RACReplaySubject subject];
    /**<2.发送信号*/
    [replaySubjct sendNext:@"我起飞了"];
    /**<3.订阅信号*/
    [replaySubjct subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];

4.RACCommand

    /**<RACCommand可直接传递信号,多用于网络封装*/
    /**<1.创建RACCommand*/
    RACCommand * command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            /**<3.发送信号*/
            [subscriber sendNext:@"我开始跑了"];

            return nil;
        }];
    }];
    /**<2.订阅RACCommand信号*/
    [command.executionSignals subscribeNext:^(id  _Nullable x) {
        /**<4.返回的不是一个对象,而是一个信号*/
        [x subscribeNext:^(id  _Nullable x) {
            NSLog(@"%@",x);
        }];
    }];

5.常用基础方法

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    TestSecondVC *testVC = [[TestSecondVC alloc] init];
    //遵守代理
    testVC.delegate = self;
    [[self
     rac_signalForSelector:@selector(sendMessageWith:)fromProtocol:@protocol(TestSecondVCDelegate)] subscribeNext:^(RACTuple * _Nullable x) {
        //RACTuple元祖
        NSLog(@"%@",x.first);
    }];
    [self.navigationController pushViewController:testVC animated:YES];
}    
第二个VC .h文件
@protocol TestSecondVCDelegate
- (void)sendMessageWith:(NSString *)str;
@end

@interface TestSecondVC : UIViewController
@property (nonatomic, assign)id<TestSecondVCDelegate>delegate;
@end
第二个VC .m文件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.delegate sendMessageWith:@"我是代理"];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    TestSecondVC *testVC = [[TestSecondVC alloc] init];
    [testVC.subject subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    [self.navigationController pushViewController:testVC animated:YES];
}
第二个VC .h文件
@interface TestSecondVC : UIViewController
@property (nonatomic, strong)RACSubject *subject;
@end
第二个VC .m文件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.subject sendNext:@"传送信息"];
}
    /**<监听键盘输入*/
    UITextField *textfield = [[UITextField alloc] init];
    @weakify(self);
    [textfield.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        @strongify(self);
        self.name = x;
    }];
    /**<监听数据变更*/
    [RACObserve(self, name) subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];

    /**<数据绑定*/
    RAC(self,name) = textfield.rac_textSignal;

    /**<监听点击事件*/
    [[[[UIButton alloc] init] rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {

    }];

    /**<KVO监听*/
    [[self.view rac_valuesForKeyPath:@"backgroundColor" observer:self] subscribeNext:^(id  _Nullable x) {

    }];

    /**<通知*/
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {

    }];

6.信号串并联

      /**<1.创建信号一*/
      RACSignal *signalOne = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
          /**<5.发送信号一*/
          [subscriber sendNext:@"信号一"];
          /**<6.信号一发送完成(必须写,代表信号一已经发送完成,否则无法触发信号二)*/
          [subscriber sendCompleted];
          return nil;
      }];
      /**<2.创建信号二*/
      RACSignal *signalTwo = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
          /**<7.发送信号二*/
          [subscriber sendNext:@"信号二"];
          /**<8.信号二发送完成*/
          [subscriber sendCompleted];
          return nil;
      }];
      /**<3.串联信号*/
      RACSignal *concatSignal = [signalOne concat:signalTwo];
      /**<4.订阅信号*/
      [concatSignal subscribeNext:^(id  _Nullable x) {
          NSLog(@"%@", x);
      }];
    /**<1.创建信号一*/
    RACSignal *signalOne = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        /**<5.发送信号一*/
        [subscriber sendNext:@"信号一"];
        return nil;
    }];
    /**<2.创建信号二*/
    RACSignal *signalTwo = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        /**<6.发送信号二*/
        [subscriber sendNext:@"信号二"];
        return nil;
    }];
    /**<3.并联信号*/
    RACSignal *concatSignal = [signalOne merge:signalTwo];
    /**<4.订阅信号*/
    [concatSignal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];

7.信号改造

    /**<1.创建信号*/
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"4点了"];
        return [RACDisposable disposableWithBlock:^{

        }];
    }];
    /**<2.信号内容改造*/
    signal = [signal map:^id _Nullable(id  _Nullable value) {
        if ([value isEqualToString:@"4点了"]) {
            return @"放学了";
        }
        return @"没放学";
    }];
    /**<3.订阅信号*/
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"放学没有?%@",x);
    }];
    /**<1.创建信号*/
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"发送信号1"];
        return [RACDisposable disposableWithBlock:^{
            
        }];
    }];
    /**<2.信号改造*/
    [[signal flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
        /**<也可以用这种方式返回信号*/
//        return [RACReturnSignal return:[NSString stringWithFormat:@"输出:%@",value]];
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [subscriber sendNext:@"发送信号2"];
            return nil;
        }];
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);//打印:发送信号2
    }];
上一篇下一篇

猜你喜欢

热点阅读