MeloS
MeloS

Reputation: 7938

MVVM with ReactiveCocoa, how does ViewModel tell View to do some one time operation?

For Example, I want my view to show a toast, the way I'm doing it now is like:

in view:

RACObserve(self.viewModel, showToast) subscribeNext:^(NSNumber *isShow) {
    if (isShow.boolValue) {
        self showToast];
    }
}

showToast is a property of my ViewModel, I don't think this way is very descriptive, is there any more standard, more elegant way to achieve this?

Upvotes: 2

Views: 115

Answers (1)

Charles Maria
Charles Maria

Reputation: 2195

To give something more descriptive, you could create a RACSubject to manually send notifications using [self.toastsSubject sendNext:@"Toast info string of some kind"].

@weakify(self)
[self.viewModel.toastsSubject subscribeNext:^(id _) {
    @strongify(self)
    [self showToast];
}

Even better, you could have showToast take a single argument (such as the content of the toast), then you don't need to use @weakify and @strongify, instead could you lift the signal directly using rac_liftSelector.

[self rac_liftSelector:@selector(showToast:)
      withSignals:self.viewModel.toastsSubject, nil];

Upvotes: 1

Related Questions