ReactiveCocoa初體驗(yàn)

最近一個(gè)項(xiàng)目 嘗試開(kāi)始使用ReactiveCocoa. 項(xiàng)目結(jié)束了 簡(jiǎn)單記錄下 項(xiàng)目中的應(yīng)用.

Case 0.基本款 - subscribeNext 應(yīng)用:VC中綁定viewModel里的error => 進(jìn)行錯(cuò)誤處理
@weakify(self);
[RACObserve(self.viewModel, error) subscribeNext:^(id x) {
        @strongify(self);
        if (self.viewModel.error) {
            [self showExceptionEmptyView:self.viewModel.error];
        }
    }];
Case 1.RAC&combineLatest&reduce 應(yīng)用:關(guān)聯(lián)登錄頁(yè)面中的登錄按鈕與用戶(hù)名&密碼框中輸入的內(nèi)容
RAC(self.sureBtn,enabled) = [RACSignal combineLatest:@[self.passWordTextField.rac_textSignal,
                                                       self.surePassWordTextField.rac_textSignal,
                                                       self.codeTextField.rac_textSignal
                                                      ]
                                               reduce:^(NSString *passWord, NSString *surePassWord, NSString *codeText){
                                                 BOOL enabled = (passWord.length > 0 && surePassWord.length > 0 && codeText.length > 0)? YES:NO;
                                                 self.sureBtn.backgroundColor = enabled ? [UIColor colorWithHex:0x008EFF]:[UIColor colorWithHex:0x797979];
                                                 return @(enabled);
                                               }];
Case 2.rac_command - UIControl's RAC Support Category. More convenient to write the action code of UIControls without delegate.
DDViolationInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DDViolationInfoCell class])
                                                            forIndexPath:indexPath];

@weakify(self);
cell.showReasonBtn.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input){
    @strongify(self);
    return [RACSignal defer:^RACSignal *(){
        // Btn Action
        return [RACSignal empty];
    }];
}];

@weakify(self);
    [self.lv2ItemPriceTextField.rac_textSignal subscribeNext:^(NSString *newText) {
        @strongify(self);
        NSString *tempStr = [NSString stringWithFormat:@"%0.2f", [newText floatValue]];
        self.lv2ItemModel.price = [tempStr floatValue];
    }];
Case 3.節(jié)流閥-throttle 應(yīng)用:輪詢(xún)請(qǐng)求
@weakify(self);
 self.violationInfoUpdatedSignal = [[RACObserve(self.viewModel, violationInfo) throttle:2.0] subscribeNext:^(id x) {
    @strongify(self);
    if (x) {
        if (4 == self.viewModel.violationInfo.code && self.viewModel.violationInfoCheckTimes < 10) {    // 如果是正在查詢(xún)中的狀態(tài) 間隔2秒 發(fā)起一次請(qǐng)求
            self.viewModel.violationInfoCheckTimes ++;
            [self.viewModel requestViolationInfoWithVehicle:self.viewModel.violationBasicInfo.vehicle
                                                        ein:self.viewModel.violationBasicInfo.ein
                                                        vin:self.viewModel.violationBasicInfo.vin
                                                      areas:self.viewModel.violationBasicInfo.areas];
        } else if (self.viewModel.isShowViolationCarInfoErrorPage) {   // 展示車(chē)輛信息錯(cuò)誤頁(yè)面
            [self showExceptionEmptyView:nil];
        } else {  // 查詢(xún)沒(méi)有錯(cuò)誤 & 查詢(xún)不處于查詢(xún)中的狀態(tài)    
    }
}];
// 終止信號(hào)
[self.violationInfoUpdatedSignal dispose];
最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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