I have been looking for a proper pattern for commands that can be cancelled.
Here is the scenario:
Joe user wants to log into the twitter service on their device. There is a button on the screen that says twitter and when the user taps on it, the login process starts and the text on the button changes to cancel. The login process could take a long period of time (30 seconds for example) and if the user presses the button again during this time, the existing login will be cancelled.
I have tried many different ways to make this work, but they all have a very bad smell. Here is the best solution I have come up with so far:
@property (strong, nonatomic) RACCommand *twitterLoginCommand;
@property (strong, nonatomic) RACCommand *cancelCommand;
@property (strong, nonatomic) id authenticatedUser;
@property (weak, nonatomic) RACDisposable * authenticationDisposable;
-(void) viewDidLoad {
RAC(self, twitterButton.rac_command, self.twitterLoginCommand) = [[RACObserve(self, twitterLoginCommand.executing) flatten]
map:^id(NSNumber * value) {
@strongify(self);
if (value.boolValue) {
return self.cancelCommand;
} else {
return self.twitterLoginCommand;
}
}];
}
-(RACCommand *) cancelCommand {
if (!_cancelCommand) {
@weakify(self);
_cancelCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
@strongify(self);
[self.authenticationDisposable dispose];
return [RACSignal empty];
}];
}
return _cancelCommand;
}
- (RACCommand *)twitterLoginCommand {
if (!_twitterLoginCommand) {
@weakify(self);
_twitterLoginCommand = [[RACCommand alloc]initWithSignalBlock: ^RACSignal *(UIButton *button) {
@strongify(self);
RACSignal * signal = [self twitterSignInSignal];
self.authenticationDisposable = [signal subscribeNext:^(FAUser * user) {
self.authenticatedUser = user;
}error:^(NSError *error) {
self.authenticationDisposable = nil;
}];
return signal;
}];
}
return _twitterLoginCommand;
}
- (RACSignal *)twitterSignInSignal {
//Left out, returns a signal with a user
}
Originally I had the twitter command being subscribed to in viewDidLoad and utilizing a takeUntil for the cancel signal (rather than using the disposable and subscribing with the command), but that did not appear to stop the command from executing immediately upon cancellation (the login task would remaining running until the signal processed the takeUntil).
Try this one.
_twitterLoginCommand = [[RACCommand alloc] initWithSignalBlock:^(id _) {
@strongify(self);
return [[self
twitterSignInSignal]
takeUntil:self.cancelCommand.executionSignals];
}];
RAC(self.authenticatedUser) = [self.twitterLoginCommand.executionSignals switchToLatest];
I suggest you to move command creations to init method
That worked like a charm and it all smells good. Thanks, it took me a couple days to get as far as I was, I must have missed something when I tried an approach like that last time.
You're welcome!
RAC(self.authenticatedUser) = [self.twitterLoginCommand.executionSignals switchToLatest];
if change it to
RAC(self.authenticatedUser) = [self.twitterLoginCommand.executionSignals flatten];
better ? @notxcain
Most helpful comment
Try this one.
I suggest you to move command creations to init method