我正在制作一个简单的iOS应用程序,以便学习ReactiveCocoa。我一直在使用XIB文件,但决定切换到故事板。
在我的第一个视图中,我有登录屏幕,当用户按下按钮时,viewModel执行RACCommand来验证用户并下载他的联系人列表。之后我需要从ViewController调用performSegueWithIdentifier:
来显示下载的数据。如何在ViewController中通知viewModel成功完成其操作?
以下是ViewController的代码片段:
RAC(self.viewModel, username) = self.usernameTextField.rac_textSignal;
RAC(self.viewModel, password) = self.passwordTextField.rac_textSignal;
self.loginButton.rac_command = self.viewModel.executeSignin;
来自ViewModel的片段:
////////////////////////////////IN INIT////////////////////////////////////
self.executeSignin =
[[RACCommand alloc] initWithEnabled:validAuthenticateSignal
signalBlock:^RACSignal *(id input) {
return [self executeSigninSignal];
}];
//////////////////////////////////////////////////////////////////////////
-(RACSignal *)executpsigninsignal {
return [[[self.services getAuthenticationService]
authenticationSignalFor:self.username andPassword:self.password]
//Return user if exists
flattenMap:^RACStream *(STUser *user) {
return [[[[[self services] getContactsLoadService]
contactsLoadSignalForUser:user] deliverOn:[RACScheduler mainThreadScheduler]]
//Return user contacts
doNext:^(NSArray *contacts) {
_downloadedContacts = [NSArray arrayWithArray:contacts];
}];
}];
}
我还尝试在ViewController中观察ViewModels的DownloadContacts属性:
RACSignal *contactsLoadSignal = RACObserve(self.viewModel, downloadedContacts);
[[contactsLoadSignal filter:^BOOL(NSArray *value) {
return value!=nil && value.count>0;
}]subscribeNext:^(NSArray *array) {
[self performSegueWithIdentifier:@"pushContactsList" sender:self];
}];
但这似乎不起作用,看起来确实不错。
答案 0 :(得分:1)
您可以使用命令的executionSignals
属性来执行此操作:
@weakify(self)
[executeSignin.executionSignals.switchToLatest filter:^BOOL(NSArray *value) {
return value.count>0; //nil check was redundant here
}] subscribeNext:^(NSArray *array) {
@strongify(self)
[self performSegueWithIdentifier:@"pushContactsList" sender:self];
}];