学习ReactiveCocoa中的一个疑惑

2018-07-26  本文已影响0人  wsxiaoluob

最近在学习ReactiveCocoa,惊异于响应式编程强大的能力之余,发现了一个有疑惑的点。
ReactiveCocoa的核心在于万物皆信号,响应式编程简单比喻就是多米诺骨牌,牵一发动全身。

网上找了些简单demo,大多是登录场景的小demo,输出账号校验格式再提交什么的。基本代码如下

ViewModel.h

@interface ViewModel : NSObject

@property (nonatomic, strong) NSString *account;
@property (nonatomic, strong) RACCommand *buttonCommand;

@end

ViewModel.m

- (RACCommand *)subscribeCommand {
    if (!_subscribeCommand) {
        @weakify(self);
        _subscribeCommand = [[RACCommand alloc] initWithEnabled:self.validSignal signalBlock:^RACSignal *(id input) {
                        **
                        提交账号密码
                        */
            return nil;
        }];
    }
    return _subscribeCommand;
}
- (RACSignal *)accountValidSignal {
    if (!_accountValidSignal) {
        _accountValidSignal = [RACObserve(self, account) map:^id(NSString *string) {
            return @(account.length > 5);
        }];
    }
    return _accountValidSignal;
}

ViewController.m

@implementation ViewController
- (void)viewDidLoad {
    **
    省略
    */
}
- (void)bindViewModel {
    RAC(self.viewModel, account) = self.account.rac_textSignal;
    self.submitButton.rac_command = self.viewModel.buttonCommand;
}
**
省略其他部分代码
*/
@end

很简单的demo,响应用户输入TextField,ViewModel的account可以一并更新,并且实时更新button状态。

好,看到这几行,你是不是完全没有疑问,一个很简单的MVVM的小demo而已。

我昨天也是这么想的。

今天无意间看了几行ReactiveCocoa的源码。

RACCommand.h

@interface RACCommand : NSObject
**
省略
*/
/// A signal of whether this command is currently executing.
///
/// This will send YES whenever -execute: is invoked and the created signal has
/// not yet terminated. Once all executions have terminated, `executing` will
/// send NO.
///
/// This signal will send its current value upon subscription, and then all
/// future values on the main thread.
@property (nonatomic, strong, readonly) RACSignal *executing;

/// A signal of whether this command is able to execute.
///
/// This will send NO if:
///
///  - The command was created with an `enabledSignal`, and NO is sent upon that
///    signal, or
///  - `allowsConcurrentExecution` is NO and the command has started executing.
///
/// Once the above conditions are no longer met, the signal will send YES.
///
/// This signal will send its current value upon subscription, and then all
/// future values on the main thread.
@property (nonatomic, strong, readonly) RACSignal *enabled;
**
省略
*/
@end

发现ReactiveCocoa框架里定义的属性,几乎都定义为RACSignal类型,仔细想了下,确实这样更符合ReactiveCocoa的核心思想,万物皆信号。

看完这个之后,我决定把上面的demo改一改:

ViewModel.h

@interface ViewModel : NSObject

@property (nonatomic, strong) RACSignal<NSString *> *account;

@property (nonatomic, strong) RACCommand *command;

@end

ViewModel.m

@interface 
@end
@implementation
- (RACCommand *)subscribeCommand {
    if (!_subscribeCommand) {
        @weakify(self);
        _subscribeCommand = [[RACCommand alloc] initWithEnabled:self.validSignal signalBlock:^RACSignal *(id input) {
                        **
                        提交账号密码
                        */
            return nil;
        }];
    }
    return _subscribeCommand;
}
- (RACSignal *)accountValidSignal {
    if (!_accountValidSignal) {
        _accountValidSignal = [RACObserve(self, account) map:^id(NSString *string) {
            return @(account.length > 5);
        }];
    }
    return _accountValidSignal;
}

ViewController.m

@implementation ViewController
- (void)viewDidLoad {
    **
    省略
    */
}
- (void)bindViewModel {
    self.vm.account = self.account.rac_textSignal;
    self.submitButton.rac_command = self.viewModel.buttonCommand;
}
**
省略其他部分代码
*/
@end

这样看代码是不是更简洁,更直观。

很多人会问,那ViewModel的account值怎么从外部set呢?

这就牵涉到整体框架的数据流转问题了。
大家知道,数据的产生,基本上有两个源头,一个是网络请求,一个是用户的action,在整个APP的开发过程中,其实我们就是一直在这两个源头的中间,做数据转换和处理工作。

那这么写有没有问题呢?

有。

MVVM相较于MVC来说,不仅是解决了Controller臃肿的问题,也解决了Controller无法测试的问题,ViewModel是可以测试的。
那么如果把ViewModel中的所有属性转化成RACSignal类型的话,测试就无法进行了,这在选型的时候是个很重要的问题。

上一篇下一篇

猜你喜欢

热点阅读