iOS Responsive programming Framework Reactivecocoa (RAC) Use example

Source: Internet
Author: User

Reactivecocoa is an implementation framework for responsive programming (FRP) in iOS, and its open source address is: https://github.com/ReactiveCocoa/ReactiveCocoa#; read several articles on the Internet, Feeling theory, but the code is still not understand, so I put it on GitHub document some of the use of the classic examples of implementation, the project can be directly moved in the past, with the proficiency of the re-reading source code is also more easily understood.


Example 1. The member variable of the listener is changed, and when the member variable value is changed, some things are triggered.

This is actually the iOS KVO mechanism used by the scene, using the KVO implementation, usually there are three steps: 1, to the object's member variables to add listening, 2, to implement a listening callback, 3, to suppress the monitoring, and through the RAC can be directly implemented, RAC callback is implemented by block, similar to procedural programming, The context is also easier to understand.

Scenario: The current class has a member variable NSString *input, which sends a request when its value is changed.

Realize:

[Racobserve (self, input)    subscribenext:^ (nsstring* x) {request        (x);//Send a request   }];

Each time the input value is modified, the block is called and the modified value is passed in as a parameter.

Scenario: In the above scenario, the request is made when the user enters a value that starts with 2.

Realize:

[[Racobserve (Self, input)     filter:^ (nsstring* value) {         if ([Value hasprefix:@ "2"]) {             return YES;         } else {             return NO;         }     }]     subscribenext:^ (nsstring* x) {request        (x);//Send a request    }];

Scene: The above scene is to listen to their own member variables, if you want to listen to Uitextfield input value changes, the framework has also been encapsulated can replace the system callback

Realize:

[[Self.priceInput.rac_textSignal     filter:^ (nsstring *str) {         if (Str.integervalue >) {             return YES;         } else {             return NO;         }     }]     subscribenext:^ (NSString *str) {<span style= "White-space:pre" ></span>request (x);//Send a request
}];

Example 2. Monitor multiple variable changes at the same time, making the button clickable when these variables meet certain conditions

Scene: Button listens to two input boxes with values and a member variable value, button is clickable when input box has input and member variable is true

Realize:

RAC (self.paybutton,enabled) = [racsignal                                   combinelatest:@[self.priceinput.rac_textsignal,                                                Self.nameInput.rac_textSignal,                                                racobserve (self, isconnected)                                                ]                                   reduce:^ (nsstring *price, NSString * Name, NSNumber *connect) {                                   return @ (price.length > 0 && name.length > 0 && [Connect boolvalue]); c6/>}];

Scenario: When the above conditions are met, the request is sent directly

Realize:

[[Racsignal                                   combinelatest:@[self.priceinput.rac_textsignal,                                                self.nameInput.rac_textSignal,                                                Racobserve (self, isconnected)                                                ]                                   reduce:^ (nsstring *price, NSString *name, NSNumber *connect) {                                   return @ ( Price.length > 0 && name.length > 0 &&! [Connect Boolvalue];                                   }]                             subscribenext:^ (NSNumber *res) {                                 if ([res boolvalue]) {                                     NSLog (@ "XXXXX send Request");                                 }                             ];

Example 3. Similar to generating production-consumption

Scenario: Each time a user enters a character in TextField, there is no other input within 1 seconds to send a request. The character change trigger event in TextField has been shown in Example 1, where the method of touch method is implemented, and the 1-second delay is implemented in this method.

Realize:

-(void) showloading {    [self.loadingdispose dispose];//last signal has not been processed, cancel it (less than 1 seconds from the last generation)    @weakify (self);    Self.loadingdispose = [[[Racsignal createsignal:^racdisposable * (id<racsubscriber> subscriber) {        [ Subscriber sendcompleted];        return nil;    }] DELAY:1]//Delay one second    subscribecompleted:^{        @strongify (self);        DoRequest ();        Self.loadingdispose = nil;    }];}

The code above looks busty, but the following snippet of similar code is quite easy to understand:

[Self.loadingdispose dispose];        Racsignal *loggingsignal = [racsignal createsignal:^ racdisposable * (id<racsubscriber> subscriber) {//BLOCK_1        subscriptions++;        [Subscriber sendnext:@ "MyTest"];        [subscriber sendcompleted];        return nil;    }];        loggingsignal = [Loggingsignal delay:10];        Self.loadingdispose = [Loggingsignal subscribenext:^ (nsstring* x) {//block_2        NSLog (@ "%@", x);        NSLog (@ "Subscription%u", subscriptions);    }];        Self.loadingdispose = [loggingsignal subscribecompleted:^{//block_3        NSLog (@ "Subscription%u", subscriptions);    } ];

Loggingsignal each time the subscriibenext:^ (ID x) or subscribecompleted:^ method is called (12 rows and 17 rows), it creates a parameter that is passed in and block_1 is triggered, and block_ Sendnext in 1: The method calls the corresponding block_2 in the subscriibenext:^, and sendcompleted in Block_1 calls subscribecompleted: the corresponding block_3


iOS Responsive programming Framework Reactivecocoa (RAC) Use example

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.