【问题标题】:Using Reactivecocoa Events call only once使用 Reactivecocoa 事件只调用一次
【发布时间】:2016-07-14 12:00:47
【问题描述】:

我有这个简单的用户登录信号。

-(RACSignal *)signInSignal {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    [self.signInService
     signInWithUsername:self.usernameTextField.text
     password:self.passwordTextField.text
     complete:^(BOOL success) {
         if(success)
         {
             [subscriber sendNext:@(success)];
             [subscriber sendCompleted];
        }
         else
             [subscriber sendError:nil];
     }];
    return nil;
}];
}

对于我的按钮

[[[[self.signInButton
    rac_signalForControlEvents:UIControlEventTouchUpInside]
   doNext:^(id x) {
       NSLog(@"In do next");
 self.signInButton.enabled = NO;
 self.signInFailureText.hidden = YES;
   }]
flattenMap:^id(id x) {
   NSLog(@"flatten map");
   return [self signInSignal];
}]
 subscribeNext:^(NSNumber *signedIn) {
     NSLog(@"In subscribe");
     self.signInButton.enabled = YES;
     self.signInFailureText.hidden = 1;
    [self performSegueWithIdentifier:@"signInSuccess" sender:self];
 } error:^(NSError *error) {
     self.signInButton.enabled = YES;
     self.signInFailureText.hidden = 0;
 }];

它运行良好,直到出现错误,所以我更改密码文本并按下登录按钮,但它什么也没做,这意味着它只调用一次(启用登录按钮)

【问题讨论】:

    标签: ios objective-c reactive-cocoa


    【解决方案1】:

    我使用RACCommand想出了一个答案

    RACCommand *submitCommand =
    [[RACCommand alloc] initWithEnabled:signUpActiveSignal signalBlock:^RACSignal *(id input) {
    
        return  [[[self signInSignal]
                doCompleted:^{
                    self.signInButton.enabled = YES;
                    self.signInFailureText.hidden = 1;
                    [self performSegueWithIdentifier:@"signInSuccess" sender:self];
                }] doError:^(NSError *error) {
                    self.signInButton.enabled = YES;
                    self.signInFailureText.hidden = 0;
                }];
    }];
    
    self.signInButton.rac_command = submitCommand;
    

    【讨论】:

    • @TonyHan 是的,它真的很有帮助。如果您觉得这有帮助,请点赞。这是我们的动力。 :D
    【解决方案2】:

    试试这个:

    [[[[self.signInButton
        rac_signalForControlEvents:UIControlEventTouchUpInside]
       doNext:^(id x){
            NSLog(@"In do next");
            self.signInButton.enabled = NO;
            self.signInFailureText.hidden = YES;
       }]
      flattenMap:^id(id x){
            NSLog(@"flatten map");
            return [self signInSignal];
      }]
    
     subscribeNext:^(NSNumber*signedIn){
            NSLog(@"In subscribe");
            self.signInButton.enabled =YES;
            BOOL success =[signedIn boolValue];
            self.signInFailureText.hidden = success;
            if(success){
                  [self performSegueWithIdentifier:@"signInSuccess" sender:self];
            }
     }];
    
    - (RACSignal *)signInSignal {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber){
            [self.signInService
             signInWithUsername:self.usernameTextField.text
             password:self.passwordTextField.text
             complete:^(BOOL success){
                 [subscriber sendNext:@(success)];
                 [subscriber sendCompleted];
             }];
            return nil;
        }];
    }
    

    【讨论】:

    • 这是 RayWenderlich 的示例,它运行良好,但我想使用“错误”块。你知道怎么做吗?
    • 当你sendError时,RAC会调用RACDisposable disposableWithBlock并且信号会被dispose。如果你想复用信号,你不能使用sendError.UIControl+RACSignalSupport- (void)sendError:(NSError *)e。跨度>
    猜你喜欢
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 2019-04-18
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多