您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關怎么使用ReactiveObjC的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
RAC架構框架圖
信號流程
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); }];
//監聽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;
//遍歷數組 NSArray *array = @[@"1", @"2", @"3", @"4", @"5"]; [array.rac_sequence.signal subscribeNext:^(id _Nullable x) { NSLog(@"內容-->%@", x)<br>}];
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"notification" object:nil] subscribeNext:^(NSNotification * _Nullable x) { NSLog(@"-->%@", x); }];
//監聽按鈕點擊方法的信號 //當執行完btnClickAction后會執行此訂閱 [[self rac_signalForSelector:@selector(btnClickAction:)] subscribeNext:^(RACTuple * _Nullable x) { NSLog(@"-->%@", x); }]; -(void) btnClickAction:(UIButton *)btn { NSLog(@"按鈕點擊"); }
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];
首先創建兩個信號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); }];
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); }] ;
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”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。