詳解如何使用ReactiveObjC
概述
RAC架構框架圖
信號流程
基本使用
1、基本控件
UITextField
//監聽文本輸入 [[_textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) { NSLog(@"%@",x); }]; //可根據自己想要監聽的事件選擇 [[_textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) { NSLog(@"%@",x); }]; //添加條件 -- 下面表示輸入文字長度 > 10 時才會調用subscribeNext [[_textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) { return value.length > 10; }] subscribeNext:^(NSString * _Nullable x) { NSLog(@"輸入框內容:%@", x); }];
UIButton
//監聽按鈕點擊事件 [[_btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) { NSLog(@"-->%@",x); }];
計時器(interval、delay)
//類似timer @weakify(self) self.disposable = [[RACSignal interval:2 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) { @strongify(self) NSLog(@"時間:%@", x); // x 是當前的時間 //關閉計時器 [self.disposable dispose]; }]; //延時 [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"延時2秒"]; return nil;<br>}] delay:2] subscribeNext:^(id x) { NSLog(@"-->%@",x); }];
2、監聽屬性變化
//監聽self的name屬性 [RACObserve(self, name) subscribeNext:^(id _Nullable x) { NSLog(@"屬性的改變-->%@",x); }]; [[self rac_valuesForKeyPath:@"name" observer:self] subscribeNext:^(id _Nullable x) { NSLog(@"屬性的改變-->%@", x); }]; //此處RAC宏相當於讓_label訂閱瞭_textField的文本變化信號 //賦值給label的text屬性 RAC(_label, text) = _textField.rac_textSignal;
3、遍歷數組和字典
//遍歷數組 NSArray *array = @[@"1", @"2", @"3", @"4", @"5"]; [array.rac_sequence.signal subscribeNext:^(id _Nullable x) { NSLog(@"內容-->%@", x)<br>}];
4、監聽 Notification 通知事件
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"notification" object:nil] subscribeNext:^(NSNotification * _Nullable x) { NSLog(@"-->%@", x); }];
5、代替Delegate代理
//監聽按鈕點擊方法的信號 //當執行完btnClickAction後會執行此訂閱 [[self rac_signalForSelector:@selector(btnClickAction:)] subscribeNext:^(RACTuple * _Nullable x) { NSLog(@"-->%@", x); }]; -(void) btnClickAction:(UIButton *)btn { NSLog(@"按鈕點擊"); }
二、RAC常用類
RACSignal
RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) { [subscriber sendNext:@"🍺🍺🍺🍺🍺🍺🍺"]; [subscriber sendCompleted]; return [RACDisposable disposableWithBlock:^{ NSLog(@"銷毀瞭🍺🍺🍺"); }]; }]; [signal subscribeNext:^(id _Nullable x) { NSLog(@"%@",x); }];
RACSubject(可發送信號也可以訂閱信號)
RACTuple(元組)– 其內部就是封裝瞭數組,用起來跟數組差不多
//通過定值創建RACTuple RACTuple *tuple = [RACTuple tupleWithObjects:@"1", @"2", @"3", nil]; //利用 RAC 宏快速封裝 RACTuple *tuple2 = RACTuplePack(@"1", @"2", @"3"); //從別的數組中獲取內容 RACTuple *tuple3 = [RACTuple tupleWithObjectsFromArray:@[@"1", @"2", @"3"]]; NSLog(@"元組-->%@", tuple3[0]); NSLog(@"第一個元素-->%@", [tuple3 first]); NSLog(@"最後一個元素-->%@", [tuple3 last]);
RACMulticastConnection — 用於當一個信號,被多次訂閱時,為瞭保證創建信號時,避免多次調用創建信號中的block
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) { [subscriber sendNext:@"signal1-->🍺🍺🍺🍺🍺🍺🍺"]; [subscriber sendCompleted]; return [RACDisposable disposableWithBlock:^{ NSLog(@"signal1銷毀瞭"); }]; }]; RACMulticastConnection *connection = [signal1 publish]; [connection.signal subscribeNext:^(id _Nullable x) { NSLog(@"subscribeNext-->1"); }]; [connection.signal subscribeNext:^(id _Nullable x) { NSLog(@"subscribeNext-->2"); }]; [connection.signal subscribeNext:^(id _Nullable x) { NSLog(@"subscribeNext-->3"); }]; [connection connect];
RACCommand — 可以監聽信號的狀態等
NSString *input = @"執行"; RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) { NSLog(@"input-->%@",input); return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) { [subscriber sendNext:@"🍺🍺🍺🍺🍺🍺🍺"]; [subscriber sendError:[NSError errorWithDomain:@"error" code:-1 userInfo:nil]]; // [subscriber sendCompleted]; return [RACDisposable disposableWithBlock:^{ NSLog(@"signal銷毀瞭"); }]; }]; }]; [command.executionSignals subscribeNext:^(RACSignal * _Nullable x) { NSLog(@"executionSignals-->%@",x); [x subscribeNext:^(id _Nullable x) { NSLog(@"executionSignals-->subscribeNext-->%@",x); }]; }]; [[command.executionSignals switchToLatest] subscribeNext:^(id _Nullable x) { NSLog(@"switchToLatest-->%@",x); }]; [command.executing subscribeNext:^(id _Nullable x) { NSLog(@"executing-->%@",x); }]; [command.errors subscribeNext:^(id _Nullable x) { NSLog(@"errors-->%@",x); }]; //開始執行 [command execute:input];
三、高級函數使用
1、連接信號、合並信號等
首先創建兩個信號signal1和signal2來演示
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) { [subscriber sendNext:@"signal1-->🍺🍺🍺🍺🍺🍺🍺"]; [subscriber sendCompleted]; return [RACDisposable disposableWithBlock:^{ NSLog(@"signal1銷毀瞭"); }]; }]; RACSignal *signal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) { [subscriber sendNext:@"signal2-->🍺🍺🍺🍺🍺🍺🍺"]; [subscriber sendCompleted]; return [RACDisposable disposableWithBlock:^{ NSLog(@"signal2銷毀瞭"); }]; }];
1.1、 concat — 當多個信號發出的時候,有順序的接收信號
一個信號signal3去監聽signal1和signal2,必須是接收signal1完後才會接收signal2
RACSignal *signal3 = [signal1 concat:signal2]; [signal3 subscribeNext:^(id _Nullable x) { NSLog(@"signal3-->%@",x); }];
1.2、 combineLatestWith — 將多個信號合並起來,並且拿到各個信號的最新的值,必須每個合並的signal至少都有過一次sendNext,才會觸發合並的信號(訂閱者每次接收的參數都是所有信號的最新值),不論觸發哪個信號都會觸發合並的信號
一個信號signal3去監聽signal1和signal2,每次回調兩個信號的最新值
RACSignal *signal3 = [signal1 combineLatestWith:signal2]; [signal3 subscribeNext:^(id _Nullable x) { NSLog(@"%@",x); }];
PS:註釋signal1的sendNext方法,直接銷毀瞭,所以每個singal必須有sendNext方法
1.3、 then — 用於連接兩個信號,等待第一個信號完成,才會連接then返回的信號
RACSignal *signal3 = [signal1 then:^RACSignal * _Nonnull{ return signal2; }]; [signal3 subscribeNext:^(id _Nullable x) { NSLog(@"signal3-->%@",x); }];
1.4、 merge — 把多個信號合並為一個信號來監聽,任何一個信號有新值的時候就會調用
一個信號signal3去監聽signal1和signal2,每次回調一個信號
RACSignal *signal3 = [signal1 merge:signal2]; [signal3 subscribeNext:^(id _Nullable x) { NSLog(@"signal3-->%@",x); }];
1.5、 zipWith — 把兩個信號壓縮成一個信號,隻有當兩個信號都發出信號內容時,才會觸發
一個信號signal3去監聽signal1和signal2,但必須兩個信號都有發出(不需要同時,例如signal1信號發出瞭,signal2信號等瞭10秒之後發出,那麼signal3的訂閱回調是等signal2信號發出的那一刻觸發)
RACSignal *signal3 = [signal1 zipWith:signal2]; [signal3 subscribeNext:^(id _Nullable x) { NSLog(@"signal3-->%@",x); }];
1.6、 reduce 聚合 — 把多個信號的值按照自定義的組合返回
RACSignal *signal3 = [RACSignal combineLatest:@[signal1,signal2] reduce:^id(NSString *s1 ,NSString *s2){ return [NSString stringWithFormat:@"%@ %@",s1,s2]; }]; [signal3 subscribeNext:^(id x) { NSLog(@"%@",x); }];
2、flattenMap & map 映射
flattenMap 的底層實現是通過bind實現的
map 的底層實現是通過 flattenMap 實現的
//map事例 [[_textField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) { return [NSString stringWithFormat:@"%@🍺🍺🍺🍺🍺🍺🍺",value]; }] subscribeNext:^(id _Nullable x) { NSLog(@"-->%@",x); }] ; //flattenMap事例 [[_textField.rac_textSignal flattenMap:^__kindof RACSignal * _Nullable(NSString * _Nullable value) { return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) { [subscriber sendNext:[NSString stringWithFormat:@"%@🍺🍺🍺🍺🍺🍺🍺",value]]; [subscriber sendCompleted]; return [RACDisposable disposableWithBlock:^(){}]; }]; }] subscribeNext:^(id _Nullable x) { NSLog(@"-->%@",x); }] ;
3、filter — 過濾、ignore — 忽略、distinctUntilChanged — 忽略相同
filter、ignore
//map事例 [[_textField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) { return [NSString stringWithFormat:@"%@🍺🍺🍺🍺🍺🍺🍺",value]; }] subscribeNext:^(id _Nullable x) { NSLog(@"-->%@",x); }] ; //flattenMap事例 [[_textField.rac_textSignal flattenMap:^__kindof RACSignal * _Nullable(NSString * _Nullable value) { return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) { [subscriber sendNext:[NSString stringWithFormat:@"%@🍺🍺🍺🍺🍺🍺🍺",value]]; [subscriber sendCompleted]; return [RACDisposable disposableWithBlock:^(){}]; }]; }] subscribeNext:^(id _Nullable x) { NSLog(@"-->%@",x); }] ;
distinctUntilChanged
RACSubject *subject = [RACSubject subject]; [[subject distinctUntilChanged] subscribeNext:^(id _Nullable x) { NSLog(@"-->%@",x); }]; [subject sendNext:@"123"]; [subject sendNext:@"123"]; [subject sendNext:@"123"]; [subject sendNext:@"🍺🍺🍺🍺🍺🍺"]; [subject sendNext:@"🍺🍺🍺🍺🍺🍺"]; [subject sendCompleted];
以上就是詳解如何使用ReactiveObjC的詳細內容,更多關於如何使用ReactiveObjC的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- ios使用NSProxy實現消息轉發
- Swift如何優雅的進行解包
- iOS NSCache和NSUrlCache緩存類實現示例詳解
- iOS GCD之dispatch_group_enter和dispatch_group_leave使用
- Java中null相關註解的實現