Search code examples
reactive-cocoa

Is there a ReactiveCocoa method like publish, but that forwards the latest value to new subscribers?


I often use ReactiveCocoa to track the latest value of some variable, like the text in a text field. Sometimes these latest values are actually themselves computed, in a way that multiple subscriptions causes the computations to happen multiple times. Often it would be significantly more efficient to merge these computations.

What I would like to use is RACSignal* sharedComputationLatest = complicatedLatestValue.publish.autoconnect, but then when new subscribers subscribe to the shared computation signal they are not told the latest value.

Is there a variant of publish, some sort of publishLatest, that already exists and does what I need? If not, does it already exist and have a name in .Net's Rx?

Update: It turns out that autoconnect has slightly different semantics than I thought, namely that if the number of subscribers goes to zero and then back up there is no re-connection. I needed the re-connection bit, so I implemented my own method (in an answer below).


Solution

  • Sounds like you want -replayLast.