步驟
1. 在ViewController中單向綁定數(shù)據(jù)
2. 在ViewModel做數(shù)據(jù)處理,驗(yàn)證數(shù)據(jù)降宅,與網(wǎng)絡(luò)回調(diào)處理
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{
/**<數(shù)據(jù)綁定*/
RAC(self.dataViewModel, account) = self.accountTF.rac_textSignal;
RAC(self.dataViewModel, password) = self.passwordTF.rac_textSignal;
/**<綁定信號(hào)*/
self.loginBT.rac_command = [self.dataViewModel verifyAccountAndPasswor];
/**<信號(hào)回調(diào)--正確信號(hào)*/
[[self.loginBT.rac_command executionSignals]subscribeNext:^(id _Nullable x) {
[MBProgressHUD showMessage:@"正在登陸"];
[x subscribeNext:^(id _Nullable x) {
[MBProgressHUD hideHUD];
[MBProgressHUD showSuccess:x];
}];
}];
/**<信號(hào)回調(diào)--錯(cuò)誤信號(hào)*/
[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;/**<賬號(hào)*/
@property (nonatomic, copy)NSString *password;/**密碼*/
/**
驗(yàn)證賬號(hào)密碼
@return RACCommand信號(hào)量
*/
- (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 ----------本地驗(yàn)證用戶(hù)名&&密碼----------
- (RACCommand *)verifyAccountAndPasswor{
/**<驗(yàn)證賬號(hào)*/
RACSignal *accountSignal = [RACObserve(self, account) map:^id _Nullable(NSString *str) {
return @(str.length >= verifyNumber ? YES : NO);
}];
/**<驗(yàn)證密碼*/
RACSignal *passwordSignal = [RACObserve(self, password) map:^id _Nullable(NSString *str) {
return @(str.length >= verifyNumber ? YES : NO);
}];
/**<合并賬號(hào),密碼 信號(hào)量*/
RACSignal *combineLatest = [RACSignal combineLatest:@[accountSignal, passwordSignal] reduce:^id (NSNumber *accountValue, NSNumber *passwordValue){
return @([accountValue boolValue] && [passwordValue boolValue]);
}];
/**<返回驗(yàn)證結(jié)果*/
return [[RACCommand alloc] initWithEnabled:combineLatest signalBlock:^RACSignal * _Nonnull(id _Nullable input) {
return [self logInWithAccount:self.account password:self.password];
}];
}
#pragma mark -
#pragma mark ----------模擬網(wǎng)路登陸----------
- (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(), ^{
/**<請(qǐng)求成功發(fā)送信號(hào)*/
[subscriber sendNext:@"登陸成功"];
[subscriber sendCompleted];
});
return nil;
}];
}
@end
簡(jiǎn)單的一個(gè)RAC+MVVM的Dome,可以看出將許多驗(yàn)證邏輯艘狭,請(qǐng)求邏輯挎扰,數(shù)據(jù)加工邏輯放在了VM中,邏輯更加清晰巢音,有利于在VM中單獨(dú)進(jìn)行單元測(cè)試遵倦。很大程度減少VC中的代碼量,同時(shí)使用RAC使代碼更加優(yōu)雅易讀港谊,使用信號(hào)量代替Block delegate來(lái)傳遞信息骇吭。