ReactiveCocoa使用心得

  1. 单向绑定
//model->viewmodel的model
   RAC(self.viewModel,model) = RACObserve(self, model);

在cell中对控件进行绑定,需要添加takeUntil

//注意,因为cell复用问题,所以要在每个UI控件上添加一个takeUntil:self.rac_prepareForReuseSignal
   RAC(self.viewModel.model,MobileNo) = [RACSignal merge:@[[self.phoneNumTextField.rac_textSignal takeUntil:self.rac_prepareForReuseSignal],[RACObserve(self.phoneNumTextField, text) takeUntil:self.rac_prepareForReuseSignal]]];
  1. 双向绑定
RACChannelTo(self.textField, text) = RACChannelTo(self.viewModel, someProperty)
  1. RACCommand
-(RACCommand *)openContactManagerCommand
{
   if (!_openContactManagerCommand) {
      _openContactManagerCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
         DebugLog(@"%@",input);
         return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [[LJContactManager sharedInstance] selectContactAtController:Context.rootNavViewController complection:^(NSString *name, NSString *phone) {
               [subscriber sendNext:phone];
               [subscriber sendCompleted];
            }];
            return nil;
         }];
      }];
   }
   return _openContactManagerCommand;
}

订阅:

[[self.viewModel.openContactManagerCommand execute:nil]subscribeNext:^(NSString*  _Nullable x) {
         self.phoneNumTextField.text = x;
      }];
  1. RACSubject
-(RACSubject *)openChargeRecordHandlerSbj
{
   if (!_openChargeRecordHandlerSbj) {
      _openChargeRecordHandlerSbj = [RACSubject subject];
      @weakify(self)
      [_openChargeRecordHandlerSbj subscribeNext:^(id  _Nullable x) {
         //TODO: 打开充值记录
      }];
   }
   return _openChargeRecordHandlerSbj;
}

订阅

[self.viewModel.openChargeRecordHandlerSbj sendNext:nil];
  1. NSMutableArray
    因为NSMutableArray不支持KVO,所以用另外一个方式处理:
[[self rac_valuesAndChangesForKeyPath:@"modelArr" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld observer:nil]subscribeNext:^(RACTwoTuple<id,NSDictionary *> * _Nullable x) {
      @strongify(self)
      NSMutableArray * arr = x.first;
      NSDictionary * changeDic = x.second;
      DebugLog(@"RAC---%@",changeDic);
      [self reloadCollectionView];
   }];

添加对象要用KVO

[[self mutableArrayValueForKey:@"modelArr"]addObject:model];
  1. 并发请求信号聚集
    让多个异步请求完成后再进行下一步逻辑操作
static int testInt = 0;
    RACSignal * request1Signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        testInt++;
        if (testInt%2 == 1) {
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
        }
        [[AFNetworkCommon sharedInstance]callerPostTransactionId:@"Timestamp.do" parameters:nil showActivityIndicator:NO showErrorAlterView:NO success:^(id  _Nonnull responseObject, AFNetworkCommonDataModel * _Nonnull dataModel) {
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
        } failure:nil];
        return nil;
    }];

    RACSignal * requeset2Signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber2) {
        if (testInt%2 == 1) {
            [subscriber2 sendNext:nil];
            [subscriber2 sendCompleted];
        }
        [[AFNetworkCommon sharedInstance]callerPostTransactionId:@"GetData.do" parameters:nil showActivityIndicator:NO showErrorAlterView:NO success:^(id  _Nonnull responseObject, AFNetworkCommonDataModel * _Nonnull dataModel) {
            [subscriber2 sendNext:nil];
            [subscriber2 sendCompleted];
        } failure:nil];
        return nil;
    }];
    [[RACSignal combineLatest:@[request1Signal,requeset2Signal]]subscribeNext:^(RACTuple * _Nullable x) {
        DebugLog(@"处理完成!!!!");
    }];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容