Canceled Reactive Timeout Cocoa

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 that I cannot understand is the way I could cancel the timeout .

RACSubscribable *oneSecGenerator = [RACSubscribable interval:1.0]; RACDisposable *timer = [[oneSecGenerator take:5] subscribeNext:^(id x) { NSLog(@"Tick"); }]; 
+4
source share
2 answers

I think I found a solution. The trick is to combine the cancel signal into a tick signal, then take X-samples. End subscribers will receive the following event each time the tick signal goes off and ends when the take is complete. Cancellation can be accomplished by sending an error to 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 the cancellation, do the following.

 [cancelTimer sendError:nil]; // nil or NSError 
+4
source

There is also a TakeUntil statement that does exactly what you want: passes events from the stream until another produces a value.

+3
source

All Articles