I want to implement a countdown timer using Reactive Cocoa in iOS. The timer should run for X seconds and do something in every second. The part I cannot figure out is the way I could cancel the timeout .
RACSubscribable *oneSecGenerator = [RACSubscribable interval:1.0];
RACDisposable *timer = [[oneSecGenerator take:5] subscribeNext:^(id x) {
NSLog(@"Tick");
}];
I think, I found the solution. The trick is to merge the cancel signal into the tick signal, then take X samples. The final subscribers will receive a next event every time the tick signal ticks and completed when the 'take' is finished. Cancellation can be implemented by sending error on the cancel timer.
__block RACSubject *cancelTimer = [RACSubject subject];
RACSubscribable *tickWithCancel = [[RACSubscribable interval:1.0] merge:cancelTimer];
RACSubscribable *timeoutFiveSec = [tickWithCancel take:5];
[timeoutFiveSec subscribeNext:^(id x) {
NSLog(@"Tick");
} error:^(NSError *error) {
NSLog(@"Cancelled");
} completed:^{
NSLog(@"Completed");
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}];
To activate cancel, one has to do the following.
[cancelTimer sendError:nil]; // nil or NSError
还有TakeUntil运算符可以完全按照您的要求执行操作:从流中继事件,直到另一个生成一个值。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.