步骤
1. 在ViewController中单向绑定数据
2. 在ViewModel做数据处理,验证数据,与网络回调处理
3. 在VC中刷新UI
1.ViewController.m中代码
//
// ViewController.m
// RACLogInDome
//
// Created by 马克吐温° on 2018/1/17.
// Copyright © 2018年 马克吐温°. All rights reserved.
//
#import "ViewController.h"
#import "ReactiveObjC.h"
#import "DataViewModel.h"
#import "MBProgressHUD+HM.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *accountTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *loginBT;
@property (nonatomic, strong)DataViewModel *dataViewModel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self bindDataAndRefreshView];
}
- (void)bindDataAndRefreshView{
/**<数据绑定*/
RAC(self.dataViewModel, account) = self.accountTF.rac_textSignal;
RAC(self.dataViewModel, password) = self.passwordTF.rac_textSignal;
/**<绑定信号*/
self.loginBT.rac_command = [self.dataViewModel verifyAccountAndPasswor];
/**<信号回调--正确信号*/
[[self.loginBT.rac_command executionSignals]subscribeNext:^(id _Nullable x) {
[MBProgressHUD showMessage:@"正在登陆"];
[x subscribeNext:^(id _Nullable x) {
[MBProgressHUD hideHUD];
[MBProgressHUD showSuccess:x];
}];
}];
/**<信号回调--错误信号*/
[self.loginBT.rac_command.errors subscribeError:^(NSError * _Nullable error) {
[MBProgressHUD showMessage:error.domain];
}];
}
- (DataViewModel *)dataViewModel{
if (!_dataViewModel) {
_dataViewModel = [[DataViewModel alloc] init];
}
return _dataViewModel;
}
@end
2.ViewModel中的代码
//
// DataViewModel.h
// RACLogInDome
//
// Created by 马克吐温° on 2018/1/17.
// Copyright © 2018年 马克吐温°. All rights reserved.
//
#import <Foundation/Foundation.h>
@class RACCommand;
@interface DataViewModel : NSObject
@property (nonatomic, copy)NSString *account;/**<账号*/
@property (nonatomic, copy)NSString *password;/**密码*/
/**
验证账号密码
@return RACCommand信号量
*/
- (RACCommand *)verifyAccountAndPasswor;
@end
//
// DataViewModel.m
// RACLogInDome
//
// Created by 马克吐温° on 2018/1/17.
// Copyright © 2018年 马克吐温°. All rights reserved.
//
#import "DataViewModel.h"
#import "ReactiveObjC.h"
#import "MBProgressHUD+HM.h"
#define verifyNumber 3
@interface DataViewModel()
@end
@implementation DataViewModel
#pragma mark -
#pragma mark ----------本地验证用户名&&密码----------
- (RACCommand *)verifyAccountAndPasswor{
/**<验证账号*/
RACSignal *accountSignal = [RACObserve(self, account) map:^id _Nullable(NSString *str) {
return @(str.length >= verifyNumber ? YES : NO);
}];
/**<验证密码*/
RACSignal *passwordSignal = [RACObserve(self, password) map:^id _Nullable(NSString *str) {
return @(str.length >= verifyNumber ? YES : NO);
}];
/**<合并账号,密码 信号量*/
RACSignal *combineLatest = [RACSignal combineLatest:@[accountSignal, passwordSignal] reduce:^id (NSNumber *accountValue, NSNumber *passwordValue){
return @([accountValue boolValue] && [passwordValue boolValue]);
}];
/**<返回验证结果*/
return [[RACCommand alloc] initWithEnabled:combineLatest signalBlock:^RACSignal * _Nonnull(id _Nullable input) {
return [self logInWithAccount:self.account password:self.password];
}];
}
#pragma mark -
#pragma mark ----------模拟网路登陆----------
- (RACSignal *)logInWithAccount:(NSString *)account password:(NSString *)password{
return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
/**<请求成功发送信号*/
[subscriber sendNext:@"登陆成功"];
[subscriber sendCompleted];
});
return nil;
}];
}
@end
简单的一个RAC+MVVM的Dome,可以看出将许多验证逻辑,请求逻辑,数据加工逻辑放在了VM中,逻辑更加清晰,有利于在VM中单独进行单元测试。很大程度减少VC中的代码量,同时使用RAC使代码更加优雅易读,使用信号量代替Block delegate来传递信息。