将参数传递给RACCommand

时间:2015-02-05 09:20:53

标签: ios reactive-cocoa frp

所以我在UIButton上使用RACCommand。点击此按钮后,用户注册即开始。它看起来像:

self->_termsOfServicesAcceptButton.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    UserModel* newUser = [[UserModel alloc] initWithUsername:self->_userNameTextField.text password:self->_passwordTextField.text email:self->_emailTextField.text firstname:self->_firstNameTextField.text lastname:self->_lastNameTextField.text];
    return [self termsOfServicesAcceptedSignal:newUser];
}

状态指示器挂钩executing信号,如:

[[self->_termsOfServicesAcceptButton.rac_command.executing deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSNumber* isSigningUp) {
    @strongify(self);
    if ([isSigningUp boolValue]) {
         [self->_signUpActivityIndicator startAnimating];
         NSLog(@" activity indicator started animating");
    }
    else {
        [self->_signUpActivityIndicator stopAnimating];
        NSLog(@" activity indicator stopped animating");
    }
}];

termsOfServicesAcceptedSignal只是现有函数的包装器,如下所示:

RACSignal* signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    [self termsOfServicesAcceptButtonPressed:newUser withCompletion:^{
        [subscriber sendNext:@(YES)];
        [subscriber sendCompleted];
    }];
    return nil;
}];
return signal;

好的,所以这很好用。现在,我想使用之前的newUserModelSignal结果来传递UserModel信息,而不是从textField.text读取它。我试过了:

self->_termsOfServicesAcceptButton.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    return [newUserModelSignal flattenMap:^RACStream *(UserModel* newUser) {
        return [self termsOfServicesAcceptedSignal:newUser];
    }];
}];

在这种意义上,newUser正确传递,注册成功,但状态指示器永远不会停止旋转,即它永远不会收到任何结束的消息。

我收集它是因为使用替换代码,RACCommand不再自动订阅termsOfServicesAcceptedSignal而是newUserModelSignal。那么我该如何解决这个问题,以便状态指示器正确地拾取executing信号?感谢。

0 个答案:

没有答案