ReactiveCocoa—RACSubject and  RACReplaySubject

cfc263fd43d875fa0dad42a51a8bbbb3.jpg

RACSubject

继承于RACSignal

// RACSubject:底层实现和RACSignal不一样。
1.调用subscribeNext订阅信号,只是把订阅者保存起来,并且订阅者的nextBlock已经赋值了。
2.调用sendNext发送信号,遍历刚刚保存的所有订阅者,一个一个调用订阅者的nextBlock
3.调用sendNext发送信号,不会保留原来的历史值,把最新的数据传递给订阅者

RACSubject *subect = [RACSubject subject];
[subect sendNext:@"数学"];

[subect subscribeNext:^(id x) {
    NSLog(@"我订阅了:%@",x);
}];
[subect sendNext:@"语文"];

[subect subscribeNext:^(id x) {
   NSLog(@"你订阅了:%@",x);
}];
[subect sendNext:@"历史"];

2016-10-12 18:02:13.298 ReactiveCocoa基础02[60479:413849] 我订阅了:语文
2016-10-12 18:02:13.298 ReactiveCocoa基础02[60479:413849] 我订阅了:历史
2016-10-12 18:02:13.299 ReactiveCocoa基础02[60479:413849] 你订阅了:历史

通常用来代替代理

ViewController.m
@interface ViewController ()

@end

@implementation ViewController


- (IBAction)click:(id)sender {

    NextViewController *nextVC = [[NextViewController alloc] init];

    // 设置代理信号
    nextVC.subect = [RACSubject subject];

    // 订阅代理信号
    [nextVC.subect subscribeNext:^(id x) {
        //点击了第二个界面
        NSLog(@"%@",x);
    }];

    // 跳转到第二个控制器
    [self presentViewController:nextVC animated:YES completion:nil];

}
NextViewController.h
@interface NextViewController : UIViewController

@property(nonatomic ,strong)RACSubject *subect;

@end
NextViewController.m
@interface NextViewController ()

@property (nonatomic ,strong)UIButton  *button;
@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:self.button];
}

//懒加载按钮
- (UIButton *)button{

    if (!_button) {
       _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = CGRectMake(0, 0, 150, 50);
        _button.backgroundColor = [UIColor whiteColor];
        [_button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_button setTitle:@"点击返回" forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(notice) forControlEvents:UIControlEventTouchUpInside];
        _button.center = self.view.center;
    }

    return _button;
}

//点击事件
-(void)notice{

    [self dismissViewControllerAnimated:YES completion:nil];

    if (self.subect) {
        [self.subect sendNext:@"点击了第二个界面"];
    }
}

RACReplaySubject

继承于RACSubject

区别:
RACReplaySubject可以先发送信号,在订阅信号,RACSubject就不可以
同时会保留原来的历史数据,等待订阅者激活时接收数据

RACReplaySubject *replaySubject = [RACReplaySubject subject];

// 2.发送信号
[replaySubject sendNext:@1];
[replaySubject sendNext:@2];

// 3.订阅信号
[replaySubject subscribeNext:^(id x) {

    NSLog(@"第一个订阅者接收到的数据%@",x);
}];
[replaySubject sendNext:@3];

// 订阅信号
[replaySubject subscribeNext:^(id x) {

    NSLog(@"第二个订阅者接收到的数据%@",x);
}];
[replaySubject sendNext:@4];

2016-10-12 18:11:46.766 ReactiveCocoa基础02[60909:424986] 第一个订阅者接收到的数据1
2016-10-12 18:11:46.766 ReactiveCocoa基础02[60909:424986] 第一个订阅者接收到的数据2
2016-10-12 18:11:46.767 ReactiveCocoa基础02[60909:424986] 第一个订阅者接收到的数据3
2016-10-12 18:11:46.767 ReactiveCocoa基础02[60909:424986] 第二个订阅者接收到的数据1
2016-10-12 18:11:46.767 ReactiveCocoa基础02[60909:424986] 第二个订阅者接收到的数据2
2016-10-12 18:11:46.768 ReactiveCocoa基础02[60909:424986] 第二个订阅者接收到的数据3
2016-10-12 18:11:46.782 ReactiveCocoa基础02[60909:424986] 第一个订阅者接收到的数据4
2016-10-12 18:11:46.782 ReactiveCocoa基础02[60909:424986] 第二个订阅者接收到的数据4

by 有涯sui无涯

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 之前对RAC有了一个基本的认识,了解了它的作用,以及RAC的运行机制,我们知道只要是信号(RACSignal...
    大大盆子阅读 4,518评论 0 11
  • 在RAC中最核心的类RACSiganl,搞定这个类就能用ReactiveCocoa开发了。 RACSiganl:信...
    Harely阅读 170评论 0 0
  • 前言由于时间的问题,暂且只更新这么多了,后续还会持续更新本文《最快让你上手ReactiveCocoa之进阶篇》,目...
    Karos_凯阅读 1,766评论 0 6
  • 1.ReactiveCocoa简介 ReactiveCocoa(简称为RAC),是由Github开源的一个应用于i...
    乱了夏末丶蓝了海阅读 781评论 0 0
  • 前言 很多blog都说ReactiveCocoa好用,然后各种秀自己如何灵活运用ReactiveCocoa,但是感...
    RainyGY阅读 1,372评论 0 1