Why the signal is called twice in ReactiveCocoa?

后端 未结 2 1960
春和景丽
春和景丽 2021-02-09 17:09

I\'m implementing my first code with https://github.com/ReactiveCocoa/ReactiveCocoa.

Is for login a user. The line [subscriber sendNext:user]; is called twi

2条回答
  •  孤街浪徒
    2021-02-09 17:34

    This happens because the block passed to +[RACSignal createSignal:] executes whenever a subscription to the signal is made, and your code creates two separate subscriptions:

    [login subscribeCompleted:^{ ... }];
    
    [login subscribeError:^(NSError *error) { ... }];
    

    If you only want to create a single subscription, use the method -[RACSignal subscribeError:completed:]:

    [login subscribeError:^(NSError *error) {
            [SVProgressHUD dismiss];
    
            [AppUrls alertError:LOC_ERROR_LOGING msg:error.userInfo[@"error"]];
        }
        completed:^{
            [[NSNotificationCenter defaultCenter]
             postNotificationName:NOTIFY_LOGIN_CHANGED
             object:self];
    
             [SVProgressHUD showSuccessWithStatus:LOC_OK];
    
    
             [self cancelForm];
        }];
    

提交回复
热议问题