RACCommand在項(xiàng)目中的實(shí)戰(zhàn)運(yùn)用和理解


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)大的方法了.

文獻(xiàn)參考

RACCommand英文文檔

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • RAC使用測(cè)試Demo下載:github.com/FuWees/WPRACTestDemo 1.ReactiveC...
    FuWees閱讀 6,656評(píng)論 3 10
  • 打算在項(xiàng)目中大面積使用RAC來(lái)開(kāi)發(fā),所以整理一些常用的實(shí)踐范例和比較完整的api說(shuō)明方便開(kāi)發(fā)時(shí)隨時(shí)查閱 聲明式編程...
    星光社的戴銘閱讀 5,480評(píng)論 5 49
  • 標(biāo)簽: iOS RAC 概述 ReactiveCocoa是一個(gè)函數(shù)響應(yīng)式編程框架,它能讓我們脫離Cocoa AP...
    GodyZ閱讀 7,786評(píng)論 16 97
  • 項(xiàng)目連接 前言 本項(xiàng)目的數(shù)據(jù)為抓包所得,并且都是用的本地?cái)?shù)據(jù),只作為學(xué)習(xí)用途。項(xiàng)目中所用到的appKey,為了方便...
    逆流丶而上閱讀 19,280評(píng)論 54 236
  • 如果你沒(méi)有過(guò) 深夜里壓低聲音痛哭, 你肯定不懂她的委屈和隱忍 如果你沒(méi)有過(guò), 在陌生人群中 淚水就那樣忍不住的滑落...
    M安吶閱讀 638評(píng)論 1 3

友情鏈接更多精彩內(nèi)容