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,512評論 0 11
  • 在RAC中最核心的類RACSiganl,搞定這個類就能用ReactiveCocoa開發了。 RACSiganl:信...
    Harely閱讀 164評論 0 0
  • 前言由于時間的問題,暫且只更新這么多了,后續還會持續更新本文《最快讓你上手ReactiveCocoa之進階篇》,目...
    Karos_凱閱讀 1,751評論 0 6
  • 1.ReactiveCocoa簡介 ReactiveCocoa(簡稱為RAC),是由Github開源的一個應用于i...
    亂了夏末丶藍了海閱讀 778評論 0 0
  • 前言 很多blog都說ReactiveCocoa好用,然后各種秀自己如何靈活運用ReactiveCocoa,但是感...
    RainyGY閱讀 1,361評論 0 1