I'm trying to understand how to dispose of a RACSignal that is scheduled to run on a background thread.
// Start button
@weakify(self);
[[self.startButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *sender) {
@strongify(self);
self.startButton.enabled = NO;
NSDate *startDate = [NSDate date];
RAC(self, elapsedTime) = [[[[RACSignal interval:0.1f onScheduler:
[RACScheduler schedulerWithPriority:RACSchedulerPriorityDefault]]
startWith:[NSDate date]] map:^id(id value) {
NSTimeInterval timeInterval = [(NSDate *)value timeIntervalSinceDate:startDate];
return [NSNumber numberWithDouble:timeInterval];
}] deliverOn:[RACScheduler mainThreadScheduler]];
}];
// Stop button
[[self.stopButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
self.startButton.enabled = YES;
// How do I stop the timer here...???
}];
RAC(self.timeLabel, text) = [RACObserve(self, elapsedTime) map:^id(NSNumber *number) {
NSString *string = [NSString stringWithFormat:@"%.1f sec. elapsed", number.floatValue];
return string;
}];
The above code does the following:
- bind a RACCommand to a button that starts a RACSignal
- the RACSignal is bound to an NSNumber (elapsedTime) that is sent a new value every 0.1 sec.
- finally I have a timeLabel bound to the number that let's me display on screen a timer updated every 0.1 seconds.
What I would like to do is being able to start and stop the timer when clicking the START and STOP buttons. Problem is I don't understand how to dispose of the signal.