Why the signal is called twice in ReactiveCocoa?

后端 未结 2 1894

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:39

    While sometimes this solution might be all you need, sometimes you do want to make sure the subscription block is only called once, maybe because it produces side effects. In this case, you can return the signal calling -replay:

    return [[RACSignal createSignal:^ RACDisposable *(id subscriber) {        
        [PFUser logInWithUsernameInBackground:email password:pwd block:^(PFUser *user, NSError *error) {
    
            if (error) {
                [subscriber sendError:error];
            } else {
                [subscriber sendNext:user];
    
                [subscriber sendCompleted];
            }
        }];
    
        return nil;
    }] map:^(PFUser *user) {
        return [self autoLogin:user];
    }] replay];
    

    This new, derived signal will send the the same messages or error to all subscribers. If the signal completes, and there is a new subscriber, this will immediately receive all messages as well.

提交回复
热议问题