最近一個項目 嘗試開始使用ReactiveCocoa. 項目結束了 簡單記錄下 項目中的應用.
Case 0.基本款 - subscribeNext 應用:VC中綁定viewModel里的error => 進行錯誤處理
@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 應用:關聯登錄頁面中的登錄按鈕與用戶名&密碼框中輸入的內容
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.節流閥-throttle 應用:輪詢請求
@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) { // 如果是正在查詢中的狀態 間隔2秒 發起一次請求
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) { // 展示車輛信息錯誤頁面
[self showExceptionEmptyView:nil];
} else { // 查詢沒有錯誤 & 查詢不處于查詢中的狀態
}
}];
// 終止信號
[self.violationInfoUpdatedSignal dispose];