项目中RAC初使用

情景一:如下图,当下面其中一项发生变化的时候,确认修改的按钮变成可点击状态

项目中RAC初使用_第1张图片
RACSignal *validseviceTypeField = [RACObserve(self, service_type) //RACObserve(self, service_type) 观察控制器self 的属性service_type的值变化
                                           map:^id(NSString *value) { //flattenMap,Map用于把源信号内容映射成新的内容
                                               if ([value isEqualToString:self.item.service_cate_id]) { 
                                                   return @(0); 
                                               }
                                               return @(1);
                                           }];
RACSignal *validNameField = [[self.detailView.nameLab rac_textSignal] map:^id(NSString *value) {
            if ([value isEqualToString:self.item.service_name]) { //value就是textField改变后的值,self.item.service_cate_id是初始值
                return @(0);
            }
            return @(1);
        }];
RACSignal *validPriceField = [[self.detailView.priceLab rac_textSignal] map:^id(NSString *value) {
            if ([value isEqualToString:self.item.service_price]) {
                return @(0);
            }
            return @(1);
        }];

RACSignal *havaPicSignal = [RACObserve(vc, photoIsChange) //vc是图片中服务详情图的控制器,photoIsChange是vc的一个bool属性,记录图片是否改动过
                                    map:^id(NSNumber *photoIsChange) {
                                        return @([photoIsChange integerValue]);
                                    }];
//RAC(btn, enabled) 调用set方法
//combineLatest合并各信号源Signal 得到(reduce)的结果
RAC(btn, enabled) = [RACSignal combineLatest:@[validseviceTypeField, validNameField, validPriceField, havaPicSignal] reduce:^id(NSNumber *validseviceTypeField, NSNumber *validNameField, NSNumber *validPriceField, NSNumber *validPic){
            return @([validseviceTypeField boolValue] || [validNameField boolValue] || [validPriceField boolValue] || [validPic boolValue]);
        }];
情景二:网络请求
如下是利用rac实现的网络请求“次基类”BaseViewModel(还有基类就是NetworkTools网络请求类)的post请求
+ (RACSignal *)postRequestWithUrl:(NSString *)url parameter:(NSDictionary *)params
{
    url = [NetworkTools concatUrl:url];
    params = [[NetworkTools sharedTools] addExtraParameters:params];
    
    if ([url respondsToSelector:@selector(length)] && url.length == 0) {
        return [RACSignal createSignal:^RACDisposable *(id subscriber) {
            [subscriber sendError:[CXXError errWithState:NO info:@"URL为空"]];
            [subscriber sendCompleted];
            return nil;
        }];
    }
    
        /**
         RACSignal的Subscription过程概括起来可以分为三个步骤:
        1.[RACSignal createSignal]来获得signal
        2.[signal subscribeNext:]来获得subscriber,然后进行subscription
        3.进入didSubscribe,通过[subscriber sendNext:]来执行next block
        */
     //1.[RACSignal createSignal]来获得signal
    return [RACSignal createSignal:^RACDisposable *(id subscriber{ //2.获得了subscriber,然后进行subscription
        [[NetworkTools sharedTools] POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            id success = [responseObject valueForKey:@"success"];
            if (![success isKindOfClass:NSNull.class] && [success boolValue]) {
                //3.进入didSubscribe,通过[subscriber sendNext:]来执行next block
                [subscriber sendNext:responseObject];
            } else {
                [subscriber sendError:[CXXError errWithState:NO info:responseObject[@"info"]]];
            }
            [subscriber sendCompleted];
            // 信号在释放的时候要完成的Block
            [RACDisposable disposableWithBlock:^{
                [task cancel];
            }];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            [subscriber sendError:[CXXError errWithError:error]];
            [subscriber sendCompleted];
            
            [RACDisposable disposableWithBlock:^{
                [task cancel];
            }];
        }];
        return nil;
    }];
}
当继承BaseViewModel的网络请求就如下请求:
+ (RACSignal *)unbindBankCardWithId:(NSString *)cardId{
    return [self postRequestWithUrl:@"Bankcard/unbundling_card" parameter:@{@"card_id":cardId?:@""}];
}
调用网络请求
[[[CXXBankViewModel unbindBankCardWithId:weakSelf.card_id] doNext:^(id x) {
//doNext: 执行Next之前,会先执行这个Block,doCompleted: 执行sendCompleted之前,会先执行这个Block
//执行完doNext:中的方法后,返回一个新的signal给原来的信号,并去执行subscribeNext中的相应方法
                        [weakSelf showHudInView:nil hint:@"解除银行卡中..."];
                    }] subscribeNext:^(NSDictionary *x) { //subscribeNext触发上面网络请求sendNext:方法,拿到responseObject,可参考下图signal的运行顺序
                        [weakSelf hideHud];
                        [weakSelf showHint:@"已解除绑定"];
                    } error:^(NSError *error) {
                        [weakSelf hideHud];
                        [weakSelf showHint:((CXXError *) error).info];
                    }];
附带网上资料查找的一张signal的运行顺序截图:
项目中RAC初使用_第2张图片

你可能感兴趣的:(项目中RAC初使用)