Reactivecocoa Understanding (Continuous Update) __reactivecocoa

Source: Internet
Author: User

Intro

Reactivecocoa is a powerful framework for OC. Its strength is not only in providing a lot of ways to simplify the work, but also in providing a way of thinking. For example: A login interface with a user name text box, password box, and login button. The login button can be clicked only if the User Name text box text length is greater than or equal to 6 and the Password box text length is greater than or equal to 6 o'clock. The common way to do this is to check whether the login button can be clicked whenever a text box or password box text changes. This approach focuses on meeting current needs in a variety of ways. The solution in Reactivecocoa is that the login button can be clicked = text box text length >=6 and password box text length >=6. This approach focuses on the problem itself, thinking about how to describe the requirement clearly, which is consistent with the way we think, and it's easier to understand. A more detailed description is available below.


Signal

Reactivecocoa (RAC) is a OBJECTIVE-C framework for functional-response programming (functional reactive programming). What is function-type reaction programming. Understanding this stuff makes it possible to use RAC better.

In the current programming world, the most editing languages are: process-oriented (such as C), object-oriented (such as C++/java), which are not current themes, and do not have interfaces here. Response programming differs from the above two, and it is oriented to signal flow. Signal flow, composed of a series of signals, and the signal contains some objects, is the carrier of information. The concept is not much, for example, you can understand much more.

For example, we now have the task of standing on the roadside to observe the vehicle. When a car comes in, I generate a signal that contains information about a car. When a car comes along, then a signal is generated, and this series of signals constitutes the signal flow. Haha, is not very simple. If you understand, congratulations, you have mastered the core part. However, this is too simple, simple to this stuff what use it. Don't worry, let's go on (I-_-! a lot of crap).

The thing I do is observe the vehicle, call the vehicle observer. Now another person, his task is to observe the vehicle when the point, record the time of each car, call him a time recorder. This time recorder has absolutely no need to observe the vehicle, because I am the vehicle observer, just when the car came, I told him to come to the car on the line, and then he looked at the time, and then recorded. The time recorder then produces a new signal that contains a time message. Notice that this time signal is produced from my vehicle signal, the vehicle signal map (map) , which produces a time signal.

Another man, who was an observer of the aircraft, was tasked with observing the planes flying over the sky. When a plane comes in, it generates a signal that contains information about the plane.

Again, the task is to write "airplane" when a plane flies, and write "car" when the vehicle comes over. When a plane flies over or the vehicle comes over, it generates a signal that the message is a string. This string signal is produced from my vehicle signal, as well as the aircraft observer's aircraft signal, that is, two signals to a combination (combine) into a string signal.

Mapping and grouping are the most basic ways of signaling operations. We can see that my observation vehicle signal can be used many times, the vehicle signal as long as I produce once, everyone can use, they have no need to observe the vehicle, as long as the focus on their own task on the line, to avoid duplication of labor. This means that the signal can be used many times, the mapping or combination operation is to generate a new signal, and does not affect the original signal.

You can derive a variety of actions, such as filtering (filter), by mapping or combining. For example, a Mercedes-Benz Observer, when my vehicle signal occurred, I told the Mercedes-Benz Observer, and then the person based on the vehicle's brand to judge, if it is a Mercedes-Benz, then generate vehicle signals, if not Mercedes-Benz, then do nothing. At this time, the Mercedes-Benz observers are also the vehicle signal, and all vehicles are Mercedes-Benz. The vehicles I generate are all brand cars. This is filtering, which is actually a new signal generated by the mapping (not every signal of my vehicle's observer generates a new signal, but only for the particular information of the Mercedes-Benz). Other signals are ignored).


Signal Status

The racsignal is used to represent the signal flow in the RAC. Signal has three states: normal state, complete state, Error state.

Normal state: Waiting for the next signal to come. (next) Completion status: The task is complete and no further signal will be generated. For example, my vehicle observer after work, the task has been completed, even if there are vehicles coming, I will ignore. (complete) Error status: An error has been generated and no further signal will be generated. For example, I suddenly sick, have to go to the hospital, of course, will not continue to observe the vehicle. (error)

Signal Subscriptions

There are three states of signals, if we are interested in the signal, we can subscribe to it: "Hey, I'm interested in your next signal, if it happens, remember to let me know," and all we have to do is define the block to execute when the new signal is generated:

  [Signal subscribenext:^ (ID value) {
     /* *  here is the Next subscription to signal, and whenever it has a signal, the block is called * the  argument of the blocks: value is the information that the signal carries.
     */
  }];

For example, for my observation of the vehicle signal subscription:

  Racsignal *signalcar = Generate observation signal of vehicle
  [Signalcar subscribenext:^ (id car) {
    NSLog ("%@", car);//Whenever a new signal is generated from Signalcar , the car's information is printed here.
  }];

Corresponding to the three states of the signal, there are three subscription methods:

  -Subscribenext: (void (^) (id x)) nextblock;
  -Subscribecompleted: (void (^) (void)) Completedblock;
  -Subscribeerror: (void (^) (nserror *error)) Errorblock;

The return value is deliberately ignored and you don't need to know about it for the time being, which doesn't affect your use.


several common methods provided by RAC

The purpose of this article is to lead the way, so here are only a few common ways to make you feel a little bit more attractive. The method of RAC begins with a RAC, and then you see that the RAC provides a lot of handy support for Uikit to see:

A) Uialertview+racsignalsupport

The RAC provides Uialertview with the-(racsignal *) Rac_buttonclickedsignal method, which creates a signal for the button click event of the Warning box.

Usage is as follows:

  Uialertview *alertview = [[Uialertview alloc] Initwithtitle:nil message:@ "RAC Demo" Delegate:nil cancelbuttontitle:@ "Cancel "otherbuttontitles:@", nil];
  [[Alertview rac_buttonclickedsignal] subscribenext:^ (ID x) {
    if ([x integervalue] = = 1) {
      NSLog (@ "clicked OK");
    } else {
      NSLog (@ "clicked Cancel");
  [Alertview show];

Isn't it cool? Mom doesn't have to worry about me writing all kinds of annoying delegate anymore. The RAC also provides this method for the Uiactionsheet class.


b) Uitextfield (Racsignalsupport)

The RAC provides the uitextfiled-(racsignal *) Rac_textsignal method. This method creates a signal for the text box, and whenever the text of the text box changes, the signal is send next.

Like what:

  [TextField rac_textsignal] subscribenext:^ (NSString *newtext) {
    NSLog (@ "%@", NewText);
  ];
RAC also provides this method for Uitextview.


c) Nsnotificationcenter (Racsupport)

The RAC provides a method for Nsnotificationcenter:-(Racsignal *) Rac_addobserverforname: (NSString *) Notificationname object: (ID) object This signal will send this notice to the relevant nsnotification each time the corresponding notice is issued. Like what:

  [[[[Nsnotificationcenter Defaultcenter] rac_addobserverforname:uikeyboardwillshownotification Object:nil] subscribenext:^ (nsnotification *keyboardwillshownotifcation) {
    NSLog (@ "Keyboard presentation notice");
  

Ah, finally no longer need to name the various notices.


D) Observer support

RAC provides a simple observer macro definition:

Racobserve (TARGET, KeyPath)

This macro definition creates a signal that signal send next when the target keypath changes. Like what:

  Racsignal *signalpersonname = Racobserve (self, person.name);
  @weakify (self);
  [Signalpersonname subscribenext:^ (NSString *newname) {
    @strongify (self);
    Self.lblName.text = NewName;
  }];

Yes, observes is so simple in the world of RAC that even removeobserver ... No need.

Ma ni Ah, the original observes can also write so simple, crazy it. Finally, we can stay away from the annoying addobserver and removeobserver.

The above @weakify (self) and @strongify (self) are designed to avoid the problem of the loop reference self in the block.

__weak __typeof__ (self) __weak_self = self; The actual content of the @weakify (self)  
__strong __typeof__ (self) self = __weak_self;//@strongify (self)

Specific can ask Niang, Google.

e) RAC macros

This is the place that I admire the RAC most. This is a magic macro.

RAC (TARGET, ...) it has two different ways:

-RAC (Target, KeyPath, Nilvalue)
-RAC (target, KeyPath)

This macro binds a signal stream to the properties of an object, and when the signal has a new signal, next, the object value in this next will be automatically assigned to Target's keypath. The first method comes with Nilvalue, which is assigned to the target's KeyPath value when the object value in next is nil.

Cases:

RAC (self, objectproperty) = objectsignal;
RAC (self, stringproperty, @ "foobar") = stringsignal;
RAC (self, integerproperty, @42) = integersignal;

Yes, there is nothing magical about it here alone, but when the RAC is combined with the MVVM, it is absolutely perfect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.