ReactiveCocoa—RACSubject|ReactiveCocoa—RACSubject and RACReplaySubject

ReactiveCocoa—RACSubject|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
【ReactiveCocoa—RACSubject|ReactiveCocoa—RACSubject and RACReplaySubject】区别:
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无涯

    推荐阅读