您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)怎么使用ReactiveObjC的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來(lái)看看吧。
RAC架構(gòu)框架圖
信號(hào)流程
UITextField
//監(jiān)聽文本輸入 [[_textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) { NSLog(@"%@",x); }]; //可根據(jù)自己想要監(jiān)聽的事件選擇 [[_textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) { NSLog(@"%@",x); }]; //添加條件 -- 下面表示輸入文字長(zhǎng)度 > 10 時(shí)才會(huì)調(diào)用subscribeNext [[_textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) { return value.length > 10; }] subscribeNext:^(NSString * _Nullable x) { NSLog(@"輸入框內(nèi)容:%@", x); }];
UIButton
//監(jiān)聽按鈕點(diǎn)擊事件 [[_btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) { NSLog(@"-->%@",x); }];
計(jì)時(shí)器(interval、delay)
//類似timer @weakify(self) self.disposable = [[RACSignal interval:2 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) { @strongify(self) NSLog(@"時(shí)間:%@", x); // x 是當(dāng)前的時(shí)間 //關(guān)閉計(jì)時(shí)器 [self.disposable dispose]; }]; //延時(shí) [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"延時(shí)2秒"]; return nil;<br>}] delay:2] subscribeNext:^(id x) { NSLog(@"-->%@",x); }];
//監(jiān)聽self的name屬性 [RACObserve(self, name) subscribeNext:^(id _Nullable x) { NSLog(@"屬性的改變-->%@",x); }]; [[self rac_valuesForKeyPath:@"name" observer:self] subscribeNext:^(id _Nullable x) { NSLog(@"屬性的改變-->%@", x); }]; //此處RAC宏相當(dāng)于讓_label訂閱了_textField的文本變化信號(hào) //賦值給label的text屬性 RAC(_label, text) = _textField.rac_textSignal;
//遍歷數(shù)組 NSArray *array = @[@"1", @"2", @"3", @"4", @"5"]; [array.rac_sequence.signal subscribeNext:^(id _Nullable x) { NSLog(@"內(nèi)容-->%@", x)<br>}];
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"notification" object:nil] subscribeNext:^(NSNotification * _Nullable x) { NSLog(@"-->%@", x); }];
//監(jiān)聽按鈕點(diǎn)擊方法的信號(hào) //當(dāng)執(zhí)行完btnClickAction后會(huì)執(zhí)行此訂閱 [[self rac_signalForSelector:@selector(btnClickAction:)] subscribeNext:^(RACTuple * _Nullable x) { NSLog(@"-->%@", x); }]; -(void) btnClickAction:(UIButton *)btn { NSLog(@"按鈕點(diǎn)擊"); }
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(可發(fā)送信號(hào)也可以訂閱信號(hào))
RACTuple(元組)-- 其內(nèi)部就是封裝了數(shù)組,用起來(lái)跟數(shù)組差不多
//通過定值創(chuàng)建RACTuple RACTuple *tuple = [RACTuple tupleWithObjects:@"1", @"2", @"3", nil]; //利用 RAC 宏快速封裝 RACTuple *tuple2 = RACTuplePack(@"1", @"2", @"3"); //從別的數(shù)組中獲取內(nèi)容 RACTuple *tuple3 = [RACTuple tupleWithObjectsFromArray:@[@"1", @"2", @"3"]]; NSLog(@"元組-->%@", tuple3[0]); NSLog(@"第一個(gè)元素-->%@", [tuple3 first]); NSLog(@"最后一個(gè)元素-->%@", [tuple3 last]);
RACMulticastConnection -- 用于當(dāng)一個(gè)信號(hào),被多次訂閱時(shí),為了保證創(chuàng)建信號(hào)時(shí),避免多次調(diào)用創(chuàng)建信號(hào)中的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 -- 可以監(jiān)聽信號(hào)的狀態(tài)等
NSString *input = @"執(zhí)行"; 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); }]; //開始執(zhí)行 [command execute:input];
首先創(chuàng)建兩個(gè)信號(hào)signal1和signal2來(lái)演示
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 -- 當(dāng)多個(gè)信號(hào)發(fā)出的時(shí)候,有順序的接收信號(hào)
一個(gè)信號(hào)signal3去監(jiān)聽signal1和signal2,必須是接收signal1完后才會(huì)接收signal2
RACSignal *signal3 = [signal1 concat:signal2]; [signal3 subscribeNext:^(id _Nullable x) { NSLog(@"signal3-->%@",x); }];
1.2、 combineLatestWith -- 將多個(gè)信號(hào)合并起來(lái),并且拿到各個(gè)信號(hào)的最新的值,必須每個(gè)合并的signal至少都有過一次sendNext,才會(huì)觸發(fā)合并的信號(hào)(訂閱者每次接收的參數(shù)都是所有信號(hào)的最新值),不論觸發(fā)哪個(gè)信號(hào)都會(huì)觸發(fā)合并的信號(hào)
一個(gè)信號(hào)signal3去監(jiān)聽signal1和signal2,每次回調(diào)兩個(gè)信號(hào)的最新值
RACSignal *signal3 = [signal1 combineLatestWith:signal2]; [signal3 subscribeNext:^(id _Nullable x) { NSLog(@"%@",x); }];
PS:注釋signal1的sendNext方法,直接銷毀了,所以每個(gè)singal必須有sendNext方法
1.3、 then -- 用于連接兩個(gè)信號(hào),等待第一個(gè)信號(hào)完成,才會(huì)連接then返回的信號(hào)
RACSignal *signal3 = [signal1 then:^RACSignal * _Nonnull{ return signal2; }]; [signal3 subscribeNext:^(id _Nullable x) { NSLog(@"signal3-->%@",x); }];
1.4、 merge -- 把多個(gè)信號(hào)合并為一個(gè)信號(hào)來(lái)監(jiān)聽,任何一個(gè)信號(hào)有新值的時(shí)候就會(huì)調(diào)用
一個(gè)信號(hào)signal3去監(jiān)聽signal1和signal2,每次回調(diào)一個(gè)信號(hào)
RACSignal *signal3 = [signal1 merge:signal2]; [signal3 subscribeNext:^(id _Nullable x) { NSLog(@"signal3-->%@",x); }];
1.5、 zipWith -- 把兩個(gè)信號(hào)壓縮成一個(gè)信號(hào),只有當(dāng)兩個(gè)信號(hào)都發(fā)出信號(hào)內(nèi)容時(shí),才會(huì)觸發(fā)
一個(gè)信號(hào)signal3去監(jiān)聽signal1和signal2,但必須兩個(gè)信號(hào)都有發(fā)出(不需要同時(shí),例如signal1信號(hào)發(fā)出了,signal2信號(hào)等了10秒之后發(fā)出,那么signal3的訂閱回調(diào)是等signal2信號(hào)發(fā)出的那一刻觸發(fā))
RACSignal *signal3 = [signal1 zipWith:signal2]; [signal3 subscribeNext:^(id _Nullable x) { NSLog(@"signal3-->%@",x); }];
1.6、 reduce 聚合 -- 把多個(gè)信號(hào)的值按照自定義的組合返回
RACSignal *signal3 = [RACSignal combineLatest:@[signal1,signal2] reduce:^id(NSString *s1 ,NSString *s2){ return [NSString stringWithFormat:@"%@ %@",s1,s2]; }]; [signal3 subscribeNext:^(id x) { NSLog(@"%@",x); }];
flattenMap 的底層實(shí)現(xiàn)是通過bind實(shí)現(xiàn)的
map 的底層實(shí)現(xiàn)是通過 flattenMap 實(shí)現(xiàn)的
//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];
感謝各位的閱讀!關(guān)于“怎么使用ReactiveObjC”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。