ReactiveCocoa初体验

2016-07-29  本文已影响58人  Nirvana_icy

最近一个项目 尝试开始使用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];
上一篇下一篇

猜你喜欢

热点阅读