Understanding Cold and Hot Signals in ReactiveCocoa (RAC)
Cold signals in ReactiveCocoa emit a complete, independent sequence for each subscriber only after subscription, while hot signals continuously push shared events regardless of listeners, causing later subscribers to miss earlier values; understanding this distinction guides proper signal choice in FRP applications.
ReactiveCocoa (RAC) is a Functional Reactive Programming (FRP) framework originally created by the GitHub team for Cocoa. FRP expresses user input as time‑varying functions, eliminating mutable state. This article is the first in a series that explains the concepts of cold and hot signals in RAC.
The notions of cold and hot signals come from the .NET Reactive Extensions (Rx). A Hot Observable is active: it pushes events continuously even if no one has subscribed, similar to mouse movement. A Cold Observable is passive: it only starts emitting when a subscriber appears. Hot observables can have multiple subscribers sharing the same data (one‑to‑many), whereas cold observables deliver a separate sequence to each subscriber (one‑to‑one).
Below is a simple RAC example that creates a signal, sends the values @1, @2, @3, and then completes. Two subscribers subscribe at different times.
RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@1];
[subscriber sendNext:@2];
[subscriber sendNext:@3];
[subscriber sendCompleted];
return nil;
}];
NSLog(@"Signal was created.");
[[RACScheduler mainThreadScheduler] afterDelay:0.1 schedule:^{
[signal subscribeNext:^(id x) {
NSLog(@"Subscriber 1 recveive: %@", x);
}];
}];
[[RACScheduler mainThreadScheduler] afterDelay:1 schedule:^{
[signal subscribeNext:^(id x) {
NSLog(@"Subscriber 2 recveive: %@", x);
}];
}];Running the program produces:
2015-08-11 18:33:21.681 RACDemos[6505:1125196] Signal was created.
2015-08-11 18:33:21.793 RACDemos[6505:1125196] Subscriber 1 recveive: 1
2015-08-11 18:33:21.793 RACDemos[6505:1125196] Subscriber 1 recveive: 2
2015-08-11 18:33:21.793 RACDemos[6505:1125196] Subscriber 1 recveive: 3
2015-08-11 18:33:22.683 RACDemos[6505:1125196] Subscriber 2 recveive: 1
2015-08-11 18:33:22.683 RACDemos[6505:1125196] Subscriber 2 recveive: 2
2015-08-11 18:33:22.683 RACDemos[6505:1125196] Subscriber 2 recveive: 3Because the signal is cold, each subscriber receives the full sequence regardless of when it subscribes.
To turn the signal into a hot one, we use publish to obtain a RACMulticastConnection and then connect it:
RACMulticastConnection *connection = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[[RACScheduler mainThreadScheduler] afterDelay:1 schedule:^{
[subscriber sendNext:@1];
}];
[[RACScheduler mainThreadScheduler] afterDelay:2 schedule:^{
[subscriber sendNext:@2];
}];
[[RACScheduler mainThreadScheduler] afterDelay:3 schedule:^{
[subscriber sendNext:@3];
}];
[[RACScheduler mainThreadScheduler] afterDelay:4 schedule:^{
[subscriber sendCompleted];
}];
return nil;
}] publish];
[connection connect];
RACSignal *signal = connection.signal;
NSLog(@"Signal was created.");
[[RACScheduler mainThreadScheduler] afterDelay:1.1 schedule:^{
[signal subscribeNext:^(id x) {
NSLog(@"Subscriber 1 recveive: %@", x);
}];
}];
[[RACScheduler mainThreadScheduler] afterDelay:2.1 schedule:^{
[signal subscribeNext:^(id x) {
NSLog(@"Subscriber 2 recveive: %@", x);
}];
}];The output is:
2015-08-12 11:07:49.943 RACDemos[9418:1186344] Signal was created.
2015-08-12 11:07:52.088 RACDemos[9418:1186344] Subscriber 1 recveive: 2
2015-08-12 11:07:53.044 RACDemos[9418:1186344] Subscriber 1 recveive: 3
2015-08-12 11:07:53.044 RACDemos[9418:1186344] Subscriber 2 recveive: 3This demonstrates the hot‑signal behavior: the signal pushes values as they occur, and subscribers that join later miss earlier values. Summarizing the characteristics:
Hot signals are active ; they emit regardless of subscription, and multiple subscribers share the same stream.
Cold signals are passive ; they start emitting only after a subscription, and each subscriber gets its own independent sequence.
Understanding these differences is essential when choosing the appropriate signal type in RAC. The next article will discuss why the distinction matters.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Meituan Technology Team
Over 10,000 engineers powering China’s leading lifestyle services e‑commerce platform. Supporting hundreds of millions of consumers, millions of merchants across 2,000+ industries. This is the public channel for the tech teams behind Meituan, Dianping, Meituan Waimai, Meituan Select, and related services.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
