RACCommand
RACCommand
:是對(duì)一個(gè)動(dòng)作的觸發(fā)條件以及它產(chǎn)生的觸發(fā)事件的封裝,最常見(jiàn)的使用場(chǎng)景為:點(diǎn)擊按鈕,發(fā)送一個(gè)網(wǎng)絡(luò)請(qǐng)求;
觸發(fā)條件:初始化RACCommand的入?yún)?code>enabledSignal就決定了RACCommand是否能開(kāi)始執(zhí)行,入?yún)nabledSignal就是觸發(fā)條件柄慰,舉個(gè)例子,一個(gè)按鈕是否能點(diǎn)擊税娜,是否能觸發(fā)點(diǎn)擊事情坐搔,就由入?yún)nabledSignal決定;
觸發(fā)事件:初始化RACCommand的另外一個(gè)入?yún)?code>(RACSignal * (^)(id input))signalBlock就是對(duì)觸發(fā)事件的封裝敬矩,RACCommand每次執(zhí)行都會(huì)調(diào)用一次signalBlock閉包概行;
RACCommand
內(nèi)部存在四個(gè)信號(hào)屬性,分別為:executionSignals
弧岳,executing
凳忙,enabled
业踏,errors
,在RACCommand
的構(gòu)造函數(shù)initWithSignalBlock
中會(huì)對(duì)以上四個(gè)信號(hào)進(jìn)行初始化涧卵,下面對(duì)它們進(jìn)行詳細(xì)介紹勤家;
executionSignals
:是RACCommand 中最重要的信號(hào),從類(lèi)型上來(lái)看柳恐,它是一個(gè)包含信號(hào)的信號(hào)
(高階信號(hào))伐脖,在每次執(zhí)行execute: 方法
時(shí),最終都會(huì)向 executionSignals 中傳入一個(gè)最新的信號(hào)乐设;
_executionSignals = [[[self.addedExecutionSignalsSubject
map:^(RACSignal *signal) {
return [signal catchTo:[RACSignal empty]];
}]
deliverOn:RACScheduler.mainThreadScheduler]
setNameWithFormat:@"%@ -executionSignals", self];
將信號(hào)中所有錯(cuò)誤轉(zhuǎn)換成RACEmptySignal
空信號(hào)對(duì)象讼庇,并派發(fā)到主線(xiàn)程;
executing
:表示RACCommand中是否有正在執(zhí)行任務(wù)的信號(hào)近尚;
_executing = [[[[[immediateExecuting
deliverOn:RACScheduler.mainThreadScheduler]
// This is useful before the first value arrives on the main thread.
startWith:@NO]
distinctUntilChanged]
replayLast]
setNameWithFormat:@"%@ -executing", self];
enabled
:表示RACCommand命令是否可以再次被執(zhí)行的信號(hào)巫俺,此信號(hào)依賴(lài)于另一個(gè)私有信號(hào)immediateEnabled
;
_immediateEnabled = [[[[RACSignal
combineLatest:@[ enabledSignal, moreExecutionsAllowed ]]
and]
takeUntil:self.rac_willDeallocSignal]
replayLast];
_enabled = [[[[[self.immediateEnabled
take:1]
concat:[[self.immediateEnabled skip:1] deliverOn:RACScheduler.mainThreadScheduler]]
distinctUntilChanged]
replayLast]
setNameWithFormat:@"%@ -enabled", self];
errors
:是RACCommand中錯(cuò)誤信號(hào)肿男;
RACMulticastConnection *errorsConnection = [[[self.addedExecutionSignalsSubject
flattenMap:^(RACSignal *signal) {
return [[signal
ignoreValues]
catch:^(NSError *error) {
return [RACSignal return:error];
}];
}]
deliverOn:RACScheduler.mainThreadScheduler]
publish];
_errors = [errorsConnection.signal setNameWithFormat:@"%@ -errors", self];
[errorsConnection connect];
從上一篇的內(nèi)容我們知道信號(hào)的使用步驟為:創(chuàng)建信號(hào),訂閱信號(hào)却嗡,發(fā)送信號(hào)內(nèi)容舶沛,訂閱者接收到信號(hào)內(nèi)容,核心部分為信號(hào)窗价,此信號(hào)是一階信號(hào)如庭,若創(chuàng)建的信號(hào)為二階信號(hào),那么發(fā)送的信號(hào)內(nèi)容就是一階信號(hào)撼港,此一階信號(hào)也是能被訂閱的坪它,下面提供一段RACCommand使用的測(cè)試代碼:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
RACCommand *command = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
NSLog(@"執(zhí)行1");
//創(chuàng)建一個(gè)一階信號(hào) 此信號(hào)會(huì)傳遞給RACCommand內(nèi)部屬性executionSignals
RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
NSLog(@"執(zhí)行3");
[subscriber sendNext:input];
return nil;
}];
return signal;
}];
//一階信號(hào) -- 信號(hào)內(nèi)容(具象數(shù)據(jù))
//二階信號(hào) -- 信號(hào)內(nèi)容(一階信號(hào))
//command.executionSignals是一個(gè)高階信號(hào)
//訂閱command的executionSignals信號(hào)
[command.executionSignals subscribeNext:^(id _Nullable x) {
//x是接收的信號(hào)內(nèi)容 是一個(gè)一階信號(hào)
[x subscribeNext:^(id _Nullable x) {
NSLog(@"執(zhí)行4");
NSLog(@"接收數(shù)據(jù) -- %@",x);
}];
NSLog(@"執(zhí)行2");
}];
//訂閱command的executing信號(hào)
[command.executing subscribeNext:^(NSNumber * _Nullable x) {
NSLog(@"executing == %@",x);
}];
//訂閱command的error信號(hào)
[command.errors subscribeNext:^(NSError * _Nullable x) {
NSLog(@"errors == %@",x);
}];
[command execute:@"發(fā)送消息"];
}
@end
第一步:創(chuàng)建RACCommand命令,同時(shí)傳入?yún)?shù)block代碼塊帝牡;
構(gòu)造方法中會(huì)對(duì)RACCommand的四種信號(hào)進(jìn)行初始化往毡;
第二步:RACCommand命令執(zhí)行execute
方法,內(nèi)部會(huì)調(diào)用保存的block代碼塊靶溜;
其中二階信號(hào)executionSignals
的創(chuàng)建實(shí)在RACCommand的構(gòu)造函數(shù)中开瞭;
最后編輯于 :2022.03.18 11:09:01
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者