1.ReactiveCocoa (RAC), a heart frame with GitHub open source for iOS and OS development, cocoa is the abbreviation for the Apple framework, so many Apple frameworks like to end with cocoa, he opens up a new era of OBJECT-C programming, His great being is in with his programming mind.
2. Programming Ideas:
Function-Responsive programming: (Functional reactive programming) abbreviation (FRP);
We know the programming idea:
2.1 Process-oriented: handle things with process as the core, step by step implementation.
2.2 Object-oriented: everything objects
2.3 Chain Programming Idea: is to put multiple operations (multiple lines of code) through the point (.) Join together to become a code that makes the code readable well.
Chained programming Features: The return value of the method is Block,block must have a return value (itself object), the block parameter (the value to be manipulated)
Responsive programming Ideas: Do not need to consider the order of calls, only to know that the results are high-cohesion, low-coupling, convenient management, similar to the butterfly effect, the occurrence of an event, will affect a lot of things, these events like a stream spread out, and then affect the results, borrowing object-oriented sentence, everything is a stream.
The role of 3.Reactivecocoa:
3.1 During iOS development, when certain events respond, some business logic needs to be handled, and these events are handled in different ways.
3.2 For example button click Use Action, ScrollView scrolling using delegate, property value change using KVO and other system provided way.
3.3 In fact, some events can be handled through RAC
3.4ReactiveCocoa provides a lot of processing for events, and the use of RAC processing events is very convenient, you can handle things, and listen to the things of the code together, so that the convenience of our management, we do not need to jump to the corresponding method inside, very consistent with our development of high cohesion, Low-coupling idea.
4. Import the REACTIVECOCOA framework using Cocoapods:
Usually use Cocoapods (plug-ins for managing third-party frameworks) to help us import.
Ps:cocoapods Tutorials
注意:
Podfile if only the pod ' Reactivecocoa ' is described, ' ~> 4.0.2-alpha-1 ', the import is unsuccessful.
Snip20150926_1.png
Error notification Information
Snip20150926_2.png
- You need to add Use_frameworks to Podfile and re-pod install to import successfully.
Common classes in 5.ReactiveCocoa: The most core class in RAC is Racsignal, which can be developed with Reactivecocoa. Racsignal: The signal class, which generally indicates that there will be data transfer in the future, as long as the data changes, Data is sent inside the signal, and the data is immediately emitted. Note: The Signal class: (racsignal), just means that when the data changes, the signal inside will emit data, he does not have the ability to send signals, but rather to the internal subscribers to send out.
RACSubscriber: Indicates the meaning of the subscriber and is used to send a signal, which is a protocol, not a class, as long as the protocol is adhered to and the implementation method can become a subscriber. The signal created by create has a subscriber that helps him send the data.
RACDisposable: Used to unsubscribe or clean up resources, it will be triggered automatically when the signal is sent or the error is sent.
使用场景: When you do not want to listen to a signal, you can proactively unsubscribe from it.
RACSubject: Racsubject: The signal provider, who can act as a signal and send a signal.
使用场景: Usually used instead of a proxy, and with it, you don't have to define a proxy.
RACReplaySubject: A subclass of Racsubject that provides the signal class repeatedly.
Common usage in 6.REACTIVECOCOA development.
6.1 Substitute proxy:
rac_signalForSelector: Used instead of proxy.
6.2 Instead of KVO:
rac_valuesAndChangesForKeyPath: Used to listen for an object's property change.
6.3 Listening Events:
rac_signalForControlEvents: Used to listen for an event.
6.4 In lieu of notification:
rac_addObserverForName: Used to listen for a notification.
6.5 Listen text box text change:
rac_textSignal: This signal is emitted whenever a change is made to the text box.
6.6 Processing when the interface has multiple requests, you need to get the data to display the interface
rac_liftSelector:withSignalsFromArray:Signals: When the incoming signals (signal array), each signal is at least sendnext once, it will trigger the method of the first selector parameter.
- Use note: Several signals, parameter one method on several parameters, each parameter corresponds to the signal emitted by the data.
The following code is my own practice:
RAC extends functionality for many of the system's classes, such as TextField provides us with a property called Rac_textsingnal, (RAC expansion has been started by RAC), singnal is the signal meaning, we subscribe to a signal, we can receive the signal sent by the message;
1. Send a message
2. Subscribe to Messages
Parameter: Some processing operation after receiving the message;
The following action causes a circular reference
[Self.myTextField.rac_textSignal subscribenext:^ (ID x) {
//
Self.myLable.text = x;
}];
Solutions (Weak references and strong references)
@weakify (self);
[Self.myTextField.rac_textSignal subscribenext:^ (ID x) {
//
@strongify (self);
Self.myLable.text = x;
}];
You can write it like that.
RAC provides us with a handy macro that copies the value of the signal sent directly to a property
RAC (self.mylable, text) = Self.myTextField.rac_textSignal;
Add a listener to lable
KVO Key-value monitoring (key--value--Observer)
Add observer to Self
Look at that property.
Focus on the old value or the new value
[Self.mytextfield addobserver:self forkeypath:@ "text" options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold Context:nil];
RAC also gives us a macro
[Racobserve (Self, count) subscribenext:^ (ID x) {
NSLOG (@ "Observer =%@", x);
}];
Create a signal yourself
Racsignal *singnal = [racsignal createsignal:^racdisposable * (id<racsubscriber> subscriber) {
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (3.0f * nsec_per_sec)), Dispatch_get_main_queue (), ^{
[Subscriber sendnext:@ "AAA"];
[Subscriber sendcompleted];
});
return [Racdisposable disposablewithblock:^{
NSLog (@ "Operation Destruction");
}];
}];
Subscribe to Signals
[Singnal subscribenext:^ (ID x) {
NSLog (@ "Custom signal:%@", X);
}];
}
Listening
-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary<nsstring *,id> * Change context: (void *) context{
//
//
NSLog (@ "KeyPath =%@, change =%@", keypath, change);
//
//}
-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{
Self.count + +;
}
Reactivecocoa First Knowledge