对象的 NSArray 的 RACSignal [英] RACSignal for an NSArray of objects

查看:62
本文介绍了对象的 NSArray 的 RACSignal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ViewController 上有一个 NSArray 的 ViewModel 对象:

I have an NSArray of ViewModel objects on my ViewController:

@property (nonatomic, strong) NSArray *viewModels;

@property (nonatomic, strong) NSArray *viewModels;

一个 ViewModel 对象看起来像这样:

A ViewModel object looks something like this:

@interface ViewModel : NSObject

@property (nonatomic) BOOL isSelected;

@end

我正在尝试在 RACCommand 的 init 方法上为 enabledSignal 创建一个 RACSignal:

I am trying to create a RACSignal for the enabledSignal on a RACCommand's init method:

- (id)initWithEnabled:(RACSignal *)enabledSignal signalBlock:(RACSignal * (^)(id input))signalBlock

如果选择了 0 个 viewModel 对象,或者选择的 viewModel 数量等于 viewModel 的总数,则此信号将告知启用命令.

This signal will tell the Command to be enabled if there are either 0 viewModel objects selected or if the number of viewModels selected is equal to the total count of the viewModels.

我可以创建一个 RACSequence,它会给我这个代码选择的 viewModel 对象:

I can create a RACSequence which will give me the viewModel objects that are selected by this code:

RACSequence *selectedViewModels = [[self.viewModels.rac_sequence

                                        filter:^BOOL(ViewModel *viewModel) {
                                            return viewModel.isSelected == YES;
                                        }]

                                       map:^id(ViewModel *viewModel) {
                                           return viewModel;
                                       }];

我将如何创建有效信号?

推荐答案

要观察所有最新的视图模型(并且 最新的视图模型)的变化,我们需要设置每次数组更改时都有新的 KVO 观察.

To observe all of the latest view models (and only the latest view models) for changes, we'll need to set up new KVO observations each time the array changes.

表示这一点的最自然方式是使用信号的信号.每个内部"信号代表对一个版本的 viewModels 的一组观察,然后我们将使用 -switchToLatest 来确保只有最新的信号生效:

The most natural way to represent this is with a signal of signals. Each "inner" signal represents a set of observations on one version of viewModels, and then we'll use -switchToLatest to ensure that only the newest signal takes effect:

@weakify(self);

RACSignal *enabled = [[RACObserve(self, viewModels)
    // Map _each_ array of view models to a signal determining whether the command
    // should be enabled.
    map:^(NSArray *viewModels) {
        RACSequence *selectionSignals = [[viewModels.rac_sequence
            map:^(ViewModel *viewModel) {
                // RACObserve() implicitly retains `self`, so we need to avoid
                // a retain cycle.
                @strongify(self);

                // Observe each view model's `isSelected` property for changes.
                return RACObserve(viewModel, isSelected);
            }]
            // Ensure we always have one YES for the -and below.
            startWith:[RACSignal return:@YES]];

        // Sends YES whenever all of the view models are selected, NO otherwise.
        return [[RACSignal
            combineLatest:selectionSignals]
            and];
    }]
    // Then, ensure that we only subscribe to the _latest_ signal returned from
    // the block above (i.e., the observations from the latest `viewModels`).
    switchToLatest];

这篇关于对象的 NSArray 的 RACSignal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆