title: RACCommand在項(xiàng)目中的實(shí)戰(zhàn)運(yùn)用和理解
date: 2016-08-09
categories: iOS
tags:
-ReactiveCocoa
{% cq %}
RACCommand作為RAC比較重要的一個(gè)部分,其作用就是得到信號(hào)指令觸發(fā)動(dòng)作執(zhí)行,一般涉及到UI交互操作.那在項(xiàng)目里我們要怎樣巧妙的使用這個(gè)利器呢?
{% endcq %}
RACCommand的你需要了解的
兩種創(chuàng)建方式
#1
_orderCreatCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
return nil;
}];
}];
#2
RACSignal *isEnableSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
return nil;
}];
_orderCreatCommand = [[RACCommand alloc] initWithEnabled:isEnableSignal signalBlock:^RACSignal *(id input) {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
return nil;
}];
}];
屬性和方法的解析
調(diào)用執(zhí)行成功返回信號(hào)的信號(hào),主線程執(zhí)行
@property (nonatomic, strong, readonly) RACSignal *executionSignals;
調(diào)用執(zhí)行當(dāng)前信號(hào)是否正在執(zhí)行返回@(NO),主線程執(zhí)行
@property (nonatomic, strong, readonly) RACSignal *executing;
默認(rèn)情況下返回@NO信號(hào),但只有一下兩種情況會(huì)返回@YES 1.上面第二種創(chuàng)建RACCommand的時(shí)候就給isEnableSignal賦值為@YES的時(shí)候 2.allowsConcurrentExecution屬性設(shè)置為 NO并且這個(gè)信號(hào)正在執(zhí)行中. 主線程執(zhí)行
@property (nonatomic, strong, readonly) RACSignal *enabled;
是否允許多個(gè)RACCommand同時(shí)執(zhí)行髓棋。
@property (atomic, assign) BOOL allowsConcurrentExecution;
執(zhí)行RACCommand的時(shí)候發(fā)送獲取的error信號(hào)
@property (nonatomic, strong, readonly) RACSignal *errors;
調(diào)用RACCommand藕赞,input為executionSignals的訂閱者發(fā)送的值
- (RACSignal *)execute:(id)input;
開(kāi)始著手項(xiàng)目實(shí)戰(zhàn)
項(xiàng)目功能需求講解一:
某訂單確認(rèn)頁(yè)面
images
點(diǎn)擊提交訂單按鈕
在VC中代碼:
這里訂閱從ViewModel返回的最終信號(hào)
//點(diǎn)擊提交訂單
[[self.commitOrderButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
STRONG
[[self.confirmOrderViewModel.orderCreatCommand execute:finalParames]
subscribeNext:^(NSDictionary *x) {
self.commitOrderButton.enabled = YES;
if ([self.confirmOrderViewModel.payWay isEqualToString:pay_downLine]{
//貨到付款處理...
} else {
//在線支付處理...
}
} error:^(NSError *error) {
[SVProgressHUD showErrorWithStatus:@"網(wǎng)絡(luò)錯(cuò)誤~"];
}];
}];
在ViewModel中代碼:
創(chuàng)建command,在input獲取從VC中調(diào)用的execute傳來(lái)的參數(shù),返回一個(gè)信號(hào).信號(hào)包裹著提交訂單網(wǎng)絡(luò)請(qǐng)求的數(shù)據(jù)返回給下一個(gè)訂閱著.
- (RACCommand *)orderCreatCommand{
_orderCreatCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSMutableDictionary *params) {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[SVProgressHUD showWithStatus:@"提交訂單..."];
[XNBaseRequest HTTPPostWithUrl:URL_SX(XNRequestOrderCreat)
Parames:parames
commplete:^(id result) {
[subscriber sendNext:payDict];
});
}
failure:^(NSError *error) {
[subscriber sendError:error];
}];
return nil;
}];
}];
return _orderCreatCommand;
}
項(xiàng)目功能需求講解二:
某訂單支付頁(yè)面
images
點(diǎn)擊確認(rèn)支付按鈕
同樣獲取按鈕點(diǎn)擊事件做你想做的事
[[self.CheckStandViewModel.payCommand execute:@"1"] subscribeNext:^(NSNumber *isSuccess) {
self.CheckStandViewModel.isOrderSuccess = isSuccess.boolValue;
} error:^(NSError *error) {
[SVProgressHUD showErrorWithStatus:@"網(wǎng)絡(luò)錯(cuò)誤~"];
}];
同樣在ViewModel里創(chuàng)建Command
... 你可以做你想自己的數(shù)據(jù)處理,在這我都不展示了
- (RACCommand *)payCommand{
WEAK
_payCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSString *payType) {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
STRONG
[SVProgressHUD showWithStatus:@"前往支付..."];
...
[XNBaseRequest HTTPPostWithUrl:url
Parames:parames
commplete:^(id result) {
...
[subscriber sendNext:@(NO)];
[subscriber sendCompleted];
failure:^(NSError *error) {
[subscriber sendError:error];
}];
return nil;
}];
}];
return _payCommand;
}
總結(jié)
在RAC中萬(wàn)物皆信號(hào),所以你需要更多的去理解信號(hào)作為一種流的方式存在.還有更多RACCommand方法等待你的發(fā)現(xiàn),至于如何更好的利用這個(gè)利器,相信你也有幾分的了解了,接下來(lái)的就需要自己不斷的摸索更多神奇而又強(qiáng)大的方法了.