ReactiveCocoa (on), ReactiveCocoa (
ReactiveCocoa common classes
1. Core class in RACRACSiganl:
RACSiganl: Signal class. It generally indicates that there will be data transfer in the future. If there is any data change, the signal will receive the data internally, and the data will be sent immediately.
Resolution:
The signal class (RACSiganl) only indicates that when the data changes, the signal sends data internally. It does not have the ability to send signals, but is sent to an internal subscriber.
By default, a signal is a cold signal, that is, if the value is changed, it will not be triggered. Only when this signal is subscribed will it become a hot signal, and if the value is changed, it will be triggered.
How to subscribe to signals: Call the subscribeNext of the signal RACSignal to subscribe.
1 // create a signal. First, save didSubscribe to the signal. It does not trigger 2 + (RACSignal *) createSignal :( RACDisposable * (^) (id <RACSubscriber> subscriber )) didSubscribe 3 4 // subscribe signal to activate the signal 5-(RACDisposable *) subscribeNext :( void (^) (id x) nextBlock 6 7 // when the signal is subscribed, that is, call signal's subscribeNext: nextBlock 8 // subscribeNext internally creates subscriber, save nextBlock to subscriber 9 // subscribeNext internally calls the didSubscribe10 11 12 of siganl // send signal 13-(void) sendNext :( id) value14 15 // call [subscriber sendNext: @ 1] In didSubscribe of siganl; 16 // The bottom layer of sendNext is actually executing the nextBlock of subscriber
Demo:
1 RACSignal * siganl = [RACSignal createSignal: ^ RACDisposable * (id <RACSubscriber> subscriber) {2 // block call time: block is called whenever a subscriber has a subscription signal. 3 // 2. send signal 4 [subscriber sendNext: @ 1]; 5 // if data is not sent, it is best to send the signal completely. Internally, [RACDisposable disposable] is automatically called to cancel the subscription signal. 6 [subscriber sendCompleted]; 7 8 return [RACDisposable disposableWithBlock: ^ {9 // block call time: when the signal is sent completely or an error is sent, the block is automatically executed, cancels the subscription signal. 10 // After the Block is executed, the current signal will not be subscribed. 11 12 NSLog (@ "signal destroyed"); 13 14}]; 15}]; 16 17 // 3. the subscription signal is activated. 18 [siganl subscribeNext: ^ (id x) {19 // block call time: Whenever a signal sends data, block.20 NSLog (@ "received data: % @", x); 21}];View Code
Print result:
2. RACSubscriber
It indicates the meaning of the subscriber, used to send signals. This is a protocol, not a class. As long as the subscriber complies with this protocol and the implementation method can become a subscriber. All signals created through create have a subscriber to help him send data.
3.
RACDisposable
It is used to cancel subscription or clear resources. It is triggered automatically when the signal is sent completely or when an error is sent.
Use Cases: When you do not want to listen to a signal, you can use it to cancel the subscription signal.
4.
RACSubject
RACSubject: A signal provider that can act as a signal and send a signal.
Use Cases: Usually used to replace the proxy. With it, you do not need to define the proxy.
RACReplaySubject: Provides the signal class repeatedly, a subclass of RACSubject.
RACReplaySubjectAndRACSubjectDifferences:
- RACReplaySubject can be used to send signals first. It cannot be used to subscribe to signals.
Scenario 1: If a signal is subscribed once, the previous value needs to be repeatedly sent, and the signal class is provided repeatedly.
Scenario 2: You can set the number of capacity to limit the number of cached values, that is, only the latest values can be slowed down.
Https://www.jianshu.com/p/87ef6720a096