How to get current value from ReactiveCocoa signal?

I have a signal returning NSNumber:

RACSignal *signal = .... 

Then in some place in the code I want to get the signal value at runtime, something like:

 NSNumber *value = [code that extracts current value of signal]; 
+6
source share
3 answers

Signals do not have the concept of โ€œcurrentโ€ meaning. The values โ€‹โ€‹are sent, then disappear - they are very ephemeral (if the theme of repetition or other tricks are not used).

You might want to subscribe to this signal. Check out the Framework Overview and the examples in README for a deeper explanation.

+6
source
  • Your current value from the ReactiveCocoa signal in the Reactive language is the subscription for this signal.

The -subscribe... methods give you access to the current and future signal values:

  [signal subscribeNext:^(id x) { NSLog(@"Current value = %@", x); }]; 
  • Another way: if you want to use this value for other values, use the combineLatest:reduce: method, for example:

     RACSignal *calculationWithCurrentValueSignal = [RACSignal combineLatest:@[signalWithCurrentValueThatNeeded, anotherSignal] reduce:^id(NSNumber *myCurrentValue, NSNumber *valueFromAnotherSignal) { //do your calculations with this values.. return newValue; }]; 
+2
source

Are the answers to the Swift version also?

A SignalPipe image that observes changes in an object property. When subscribing to a signal from several other objects, i.e. queue.queueCountSignal.observeNext({...}) , the watch block will be executed the next time the property changes. Is there a way to query the current value or call the watch function of NextBlock?

I do not want to use SignalProducer (which can be run explicitly) because it would mean that I need to collect observeNext blocks from each object that needs a signal. I also do not want to create several signal manufacturers for the same - or is it really necessary?

Here is sample code to make it more understandable

 import ReactiveCocoa class SwipingQueueWithSignals<T> : SwipingQueue<T> { override var count: Int { didSet(oldQueueCount) { let newQueueCount = self.count queueCountSignalObserver.sendNext(newQueueCount) } let queueCountSignal: Signal<Int, NoError> private let queueCountSignalObserver: Observer<Int, NoError> init() { (self.queueCountSignal, self.queueCountSignalObserver) = Signal<Int, NoError>.pipe() super.init() } } // Something like this queue.queueCountSignal. .observeNext { next in print(next) } .lookupCurrentValueNow() 
+1
source

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


All Articles