Why is SignalProducer not returning a signal?

It seems to me that I understand that all the main components of ReactiveCocoa (conceptually), understanding how to connect all parts together, are still a bit confusing.

For example, after reading the β€œSignal”, I fully expected that the SignalProducer would have only one start () method that returns a signal that you would use like this:

mySignalProducer.start().observe(myObserver) 

Instead, you must pass the observer to start (), and the SignalProducer calls the watch () function for you:

 mySignalProducer.start(myObserver) 

This means that the SignalProducer interface is much larger (more for understanding), because all changes to the monitoring function () must be duplicated when starting () (for example, startNext (), etc.).

I think there are two possibilities:

  • There are technical reasons why start () cannot just return a signal
  • I understand the concept of SignalProducer conceptually, which leads to uncertain expectations of the interface.

If so, I guess this has something to do with managing memory and supplies, which I still don't quite understand.

I'm more worried about that. Internally, my understanding of SignalProducer is mostly mapped to the Factory concept, for example:

 mySignalFactory.createSignal().observe(myObserver) 

therefore, I am surprised that we do not find start (), which returns a signal.

I would be very grateful if the community could shed light here.

Thanks!

+5
source share
1 answer

I think the main reason is that some events can be sent immediately after the manufacturer starts.

For example, if you do not like the interface of the initial series and you want to receive a signal directly at startup:

 extension SignalProducer { func getSignalFromStart() -> Signal<Value, Error> { var signal: Signal<Value, Error>! startWithSignal{ innerSignal, _ in signal = innerSignal } return signal } } 

Then you can skip some events. Try the following:

 // When property.producer starts, it will send its current value immediately let property = MutableProperty(1) property.producer.getSignalFromStart().observeValues { value in print("getSignalFromStart \(value)") // maybe not what you want, only gets 2 } property.producer.startWithValues { value in print("normal start \(value)") // this normally gets 1 and 2 } property.value = 2 
+5
source

Source: https://habr.com/ru/post/1243804/


All Articles