How do I create a delay between signal sequences in ReactiveCocoa?
cocoa, reactive-cocoa
Solution
Your code is pretty close. You don't need the "spacer" signals, you just need to put the `-delay:` calls on the first and second signals. `-concat:` will serialize the execution of the signals so that each subsequent signal starts only after its prior signal completes, and `-delay:` will postpone the delivery of its signal's completion, thus providing the required delay before the subsequent signal starts its work. You also don't need to hop into a RACSequence and back out, as `-concat:` can take a fast enumeration of signals:
NSLog(@"Starting");
NSArray *signals = @[ [first delay:1.0f], [second delay:1.0f], third ];
[[RACSignal concat:signals] subscribeCompleted:^{
NSLog(@"Done!");
}];
Problem
I have three signals which I want to evaluate in sequence with a one second delay between them. This snippet does what I want, but it's ugly: ``` RACSignal *first = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"First!"); [subscriber sendCompleted]; return nil; }]; RACSignal *second = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"Second!"); [subscriber sendCompleted]; return nil; }]; RACSignal *third = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"Third!"); [subscriber sendCompleted]; return nil; }]; NSArray *signals = @[first, [[RACSignal empty] delay:1.0f], second, [[RACSignal empty] delay:1.0f], third]; NSLog(@"Starting"); [[[signals rac_sequence].signal concat] subscribeCompleted:^{ NSLog(@"Done!"); }]; ``` And it prints out: ``` 2013-11-18 17:13:35.326 Starting 2013-11-18 17:13:35.327 First! 2013-11-18 17:13:36.328 Second! 2013-11-18 17:13:37.329 Third! 2013-11-18 17:13:37.330 Done! ```