Import Reactivecocoa Framework
- At the terminal, enter the reactive cocoa file
- Create Podfile
- Open the file
- and configure
- use_frameworks!
- Pod ' Reactivecocoa ', ' ~>4.0.2-alpha-1 '
- Installing pod install--no-repo-update
- You can put the header file into the global. h file if you encounter a header file that is not imported.
Reactivecocoa Common Classes
- Racsignal: Signal class; Racsubscriber is a protocol.
- Brief introduction
- The Racsignal class is used when data is generated
- The default is the cold signal, must subscribe to the signal class
- Racsignal Use steps
- Create a signal (a cold signal is created by default)
- Didsubscribe call: Whenever a signal is subscribed, it is called
- Didsubscribe function: Send data
- Subscription signal (to thermal signal) Subscribenext: Subscribe
- Nextblock call: Whenever the Subscriber sends the data, it calls the
- Nextblock function: Process data, show on UI
- Send Signal (sendnext:)
As long as subscribers call Sendnext, they execute Nextblock
As long as the subscription racdynamicsignal will be executed didsubscribe the precondition is racdynamicsignal, different types of signals of the subscription, processing the subscription is not the same thing racsignal use the steps:
1. Create Signal + (racsignal *) createsignal: (racdisposable * (^) (id<racsubscriber> subscriber)) Didsubscribe
2. The signal is not activated until the signal is subscribed. -(Racdisposable *) Subscribenext: (void (^) (id x)) nextblock
3. Send signal-(void) Sendnext: (ID) value
Racsignal bottom-level implementations:
1. Create the signal, first save the didsubscribe to the signal, will not be triggered.
2. When the signal is subscribed, that is to call Signal's Subscribenext:nextblock
2.2 Subscribenext internally creates the Subscriber Subscriber and saves Nextblock to Subscriber.
2.1 Subscribenext internally calls SIGANL's Didsubscribe
Call [subscriber sendnext:@1] in the didsubscribe of 3.siganl;
3.1 Sendnext The ground floor is actually the nextblock that executes subscriber.
1. Create a signal
Racsignal *siganl = [racsignal createsignal:^racdisposable * (id<racsubscriber> subscriber) {
Block call time: block is called whenever a subscriber subscribes to a signal.
2. Send a signal
[Subscriber sendnext:@1];
If the data is not sent, it is best to send a signal to complete, the internal will automatically call [racdisposable disposable] unsubscribe signal.
[Subscriber sendcompleted];
return [Racdisposable disposablewithblock:^{
Block call time: When the signal is sent or the error is sent, the block is automatically executed and the subscription signal is canceled.
After the block is executed, the current signal is not subscribed.
NSLog (@ "signal is destroyed");
}];
}];
3. The signal is not activated until the signal is subscribed.
[SIGANL subscribenext:^ (ID x) {
Block call time: blocks are called whenever a signal is emitted.
NSLog (@ "received data:%@", x);}]; 2. Racdisposable: Unsubscribe
- The subscription is automatically canceled as long as the signal is sent.
- Subscriptions that do not automatically cancel the signal as long as the subscriber is
- Call [disposable dispose]; it will be canceled.
3. Racsubject: The signal Provider can act as a signal, or act as a subscriber
- must subscribe to send signal
- usage scenarios: Usually used instead of proxies, with which he does not have to define proxies
- racsubject use steps
1. Create a signal [racsubject Subject], unlike RACSIGANL, there is no block when creating a signal.
2. Subscription signal-(racdisposable *) Subscribenext: (void (^) (id x)) nextblock
3. Send signal Sendnext: (ID) value
- Racsubject: The underlying implementation is not the same as racsignal.
1. Call the Subscribenext subscription signal, just save the Subscriber, and the subscriber's Nextblock has been assigned value.
2. Call Sendnext to send a signal, traverse all the subscribers that have just been saved, and a nextblock that invokes the Subscriber.
//1. Create signal
Racsubject *subject = [racsubject subject];
//2. Subscription signal
[subject subscribenext:^ (id x) {
//Block call time: When the signal emits a new value, it is called.
NSLog (@ "First subscriber%@", x);
}];
[subject subscribenext:^ (id x) {
//Block call time: When the signal emits a new value, it is called.
NSLog (@ "Second subscriber%@", x);
}];
//3. Send signal
[subject sendnext:@ "1"];
4. racreplaysubject: Provide the signal class repeatedly, is Racsubject subclass Racreplaysubject can send the signal first, in the subscription signal, Racsubject can not. use Scenario One ': If a signal is subscribed once, it needs to send the previous value again and again, using a repeating signal class. use Scenario two ': You can set the number of capacity to limit the number of cached values, which is only a few of the most recent.
- racreplaysubject Use steps:
1. Create a signal [racsubject subject], unlike RACSIGANL, when creating a signal without a block.
2. You can subscribe to a signal first, or you can send a signal first.
2.1 Subscription signal-(racdisposable *) Subscribenext: (void (^) (id x)) nextblock
2.2 Send signal Sendnext: ( ID) value
- Racreplaysubject: The underlying implementation is not the same as racsubject.
1. Call Sendnext to send a signal, save the value, and then traverse all the subscribers that were just saved, a nextblock that invokes the Subscriber.
2. Call the Subscribenext subscription signal, traverse all saved values, and a nextblock that invokes the subscriber
- If you want a signal to be subscribed, repeat all values before playing, and you need to send a signal before subscribing to the signal. That is, save the value at the subscription value first.
//1. Create signal
Racreplaysubject *replaysubject = [racreplaysubject subject];
//2. Send Signal
[Replaysubject sendnext:@1];
[Replaysubject sendnext:@2];
//3. Subscription signal
[replaysubject subscribenext:^ (id x) {
NSLog (@ " The first subscriber receives the data%@ ", x);
}];
//Subscription signal
[replaysubject subscribenext:^ (id x) {
NSLog (@ " The second subscriber receives the data%@ ", x);
}];
>>> racsubject Replacement Agent requirements: 1. Add a button to the current controller, modal to another controller Interface 2. Another controller view has a button, click the button, notify the current controller step one: in the second controller. h, add a RAC Subject instead of proxies.
@interface Twoviewcontroller:uiviewcontroller
@property (nonatomic, strong) Racsubject *delegatesignal;
@end
Step two: Listen to the second controller button click
@implementation Twoviewcontroller
-(ibaction) Notice: (ID) Sender {
Notify the first controller, tell it, the button is ordered
Notification Agent
Determine if the proxy signal has a value
if (self.delegatesignal) {
If you have a value, you need to notify
[Self.delegatesignal Sendnext:nil];
}
}
@end
Step three: In the first controller, listen to the Jump button, to the second controller's proxy signal assignment, and monitoring.
@implementation Oneviewcontroller
-(Ibaction) Btnclick: (ID) Sender {
Create a second controller
Twoviewcontroller *TWOVC = [[Twoviewcontroller alloc] init];
Set proxy signal
Twovc.delegatesignal = [Racsubject subject];
Subscribe to proxy signals
[Twovc.delegatesignal subscribenext:^ (ID x) {
NSLog (@ "Click on the Notification button");
}];
Jump to a second controller
[Self PRESENTVIEWCONTROLLER:TWOVC animated:yes completion:nil];
}
@end 5. Ractuple: Tuple class Racsequence: Collection class, used instead of nsarray,nsdictionary, can be used to quickly iterate through arrays and dictionaries//1. Iterating through an array
Nsarray *numbers = @[@1,@2,@3,@4];
This is actually three steps.
First step: Convert the array into a set racsequence numbers.rac_sequence
The second step: Convert the Set racsequence Racsignal signal class, numbers.rac_sequence.signal
Step three: Subscribe to the signal, activate the signal, will automatically put all the values in the collection, traversed out.
[Numbers.rac_sequence.signal subscribenext:^ (ID x) {
NSLog (@ "%@", X);
}];
2. Iterate through the dictionary and the key-value pairs that are traversed are wrapped into ractuple (tuple objects)
Nsdictionary *dict = @{@ "name": @ "HMJ", @ "age": @18};
[Dict.rac_sequence.signal subscribenext:^ (Ractuple *x) {
The Jiu Baoyuan Group, which assigns the values of the tuples, in order, to the variables within the parameters
Ractupleunpack (NSString *key, nsstring *value) = x;
Equivalent to the following wording
NSString *key = x[0];
NSString *value = x[1];
NSLog (@ "%@%@", key,value);
}];
3. Dictionary to model 3.1 oc notation NSString *filepath = [[NSBundle mainbundle] pathforresource:@ "flags.plist" oftype:nil];
Nsarray *dictarr = [Nsarray Arraywithcontentsoffile:filepath];
Nsmutablearray *items = [Nsmutablearray array];
For (Nsdictionary *dict in Dictarr) {
Flagitem *item = [Flagitem flagwithdict:dict];
[Items Addobject:item];
}
3.2 RAC notation NSString *filepath = [[NSBundle mainbundle] pathforresource:@ "flags.plist" oftype:nil];
Nsarray *dictarr = [Nsarray Arraywithcontentsoffile:filepath];
Nsmutablearray *flags = [Nsmutablearray array];
_flags = flags;
Rac_sequence Note: Call Subscribenext, do not immediately execute nextblock, but will wait for a while.
[DictArr.rac_sequence.signal subscribenext:^ (ID x) {
Using the RAC Traversal dictionary, x: Dictionary
Flagitem *item = [Flagitem flagwithdict:x];
[Flags Addobject:item];
}];
NSLog (@ "%@", Nsstringfromcgrect ([UIScreen mainscreen].bounds));
3.3 RAC Advanced notation: NSString *filepath = [[NSBundle mainbundle] pathforresource:@ "flags.plist" oftype:nil];
Nsarray *dictarr = [Nsarray Arraywithcontentsoffile:filepath];
Map: The meaning of the map, purpose: To map the original value to a new value
Array: Convert sets to arrays
The underlying implementation: when the signal is subscribed, it traverses the original value in the collection, maps it to the new value, and saves it to the new array.
Nsarray *flags = [[Dictarr.rac_sequence map:^id (id value) {
return [Flagitem Flagwithdict:value];
}] [array];
Common classes of RAC (reactive COCOA)