Search code examples
reactive-cocoareactive-swiftreactive-cocoa-5

ReactiveCocoa: Difference between binding to a Signal or a SignalProducer?


The binding operator (<~) takes a BindingSource argument, both Signal and SignalProducer conform to the protocol

I would expect an UI element that is bound to a producer wouldn't "receive events" until the producer is started in some way but this does not seem to be the case

ie

let text = MutableProperty("abc")

myLabel1.reactive.text <~ text.signal
myLabel2.reactive.text <~ text.producer

text.value = "def"

causes both labels to update.

Is this intended behavior or am I misunderstanding something?


Solution

  • This is intended behavior. You can see in the implementation of <~ that start is called explicitly (and the producer's disposal is tied to the lifetime of the binding target).

    [EDIT]

    To clarify, it doesn't make much sense to bind the producer itself to a label in the way you are imagining. What would happen if start was called multiple times to produce multiple signals? Would the label get the values from the first signal, the most recent signal, or all signals merged together? There's no intuitive way for that to work.