- 单向绑定
//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]]];
- 双向绑定
RACChannelTo(self.textField, text) = RACChannelTo(self.viewModel, someProperty)
- 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;
}];
- RACSubject
-(RACSubject *)openChargeRecordHandlerSbj
{
if (!_openChargeRecordHandlerSbj) {
_openChargeRecordHandlerSbj = [RACSubject subject];
@weakify(self)
[_openChargeRecordHandlerSbj subscribeNext:^(id _Nullable x) {
//TODO: 打开充值记录
}];
}
return _openChargeRecordHandlerSbj;
}
订阅
[self.viewModel.openChargeRecordHandlerSbj sendNext:nil];
- 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];
- 并发请求信号聚集
让多个异步请求完成后再进行下一步逻辑操作
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(@"处理完成!!!!");
}];