ReactiveCocoa 첫 경험

3349 단어
최근 프로젝트에서 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];

좋은 웹페이지 즐겨찾기