RAC初學

Coding_Physical 2022-01-07 22:31:13 阅读数:347

rac
//
// ViewController.m
// LearnRAC-01
//
// Created by maochengfang on 2021/7/3.
//
#import "ViewController.h"
#import "ReactiveObjC.h"
@interface ViewController ()
@property (nonatomic, strong) RACDisposable *subscriber;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
/*
1.創建信號
disSubScriber調用:只要一個信號被訂閱就調用
disSubScriber作用:利用subscriber來發送數據
disSubScriber能否執行 取决於信號是否被訂閱
*/
/*
RACDisposable 它是幫助我們取消訂閱
什麼時候需要取消訂閱
1.信號已發送完畢
2.信號發送失敗
*/
RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
NSLog(@"block被執行");
[subscriber sendNext:@"Hello Oliver"];
self->_subscriber = subscriber;
return [RACDisposable disposableWithBlock:^{
NSLog(@"訂閱被取消");
}];
}];
//2.訂閱信號(熱信號)
/*
nextBlock調用:只要訂閱發送數據,信號就會被調用
nextBlock作用:處理數據,展示UI
nextBlock是否額比調用,取决於訂閱著是否發送了信號
*/
RACDisposable *disposable = [signal subscribeNext:^(id _Nullable x) {
NSLog(@"訂閱信號--%@",x);
}];
/*
默認一個信號發送數據完畢之後就會主動取消訂閱
只要訂閱著還在,就不會自動取消訂閱
手動取消
*/
[disposable dispose];
}
@end

2021-07-03 10:53:03.441066+0800 LearnRAC-01[1394:32664] block被執行

2021-07-03 10:53:03.441326+0800 LearnRAC-01[1394:32664] 訂閱信號--Hello Oliver

2021-07-03 10:53:03.441448+0800 LearnRAC-01[1394:32664] 訂閱被取消

版权声明:本文为[Coding_Physical]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201072231130799.html