Function-Responsive programming

Source: Internet
Author: User

Basic methods of Reactivecocoa use

Reactivecocoa is an open source project on GitHub and is an implementation of FRP on the iOS platform. The core of the FRP is the signal, which is represented by the racsignal in the Reactivecocoa (hereinafter referred to as RAC), which is a data stream that can be bound and passed.

Can think of the signal as a faucet, but the inside is not water, but the glass ball (value), the diameter of the same as the water pipe, so that the glass ball is arranged in order, there will be no side of the situation (data are linear processing, there will be no concurrency). The tap switch is off by default and will not open unless the receiver (subscriber) is available. So as soon as a new glass ball comes in, it is automatically transmitted to the receiver. Can add a filter on the faucet, does not conform to pass, can also add a change device, the ball to meet their own needs (map). You can also combine multiple taps into a new faucet (combinelatest:reduce:) so as long as one of the taps has a glass ball coming out, this new combined faucet will get the ball.

RAC unifies the processing of KVO, UI Event, Network request, and Async work because they are essentially changes in value (values over time).

Alternative KVO

@property (strong) NSString *username; [Racobserve (Self,username) subscribenext:^ (NSString *newname) {    NSLog (@ "%@", NewName);}];

As long as the username is changed, print it out. embodies bindings and responses.

[[Racobserver (Self,username) filter:^ (NSString *newname) {   return [newName hasprefix:@ "J"];}] subscribenext:^ ( NSString *newname) {   NSLog (@ "%@", NewName);}

Add a filter

Alternative Selector

Self.button.rac_command = [[Raccommand alloc] initwithsignalblock:^ (id _) {    NSLog (@ "button was pressed!");    return [racsignal empty];}];

The implementation of the stream

RAC (self, timelabel.text) = [[[Racsignal interval:1 Onscheduler:[racscheduler CurrentScheduler]] Startwith:[nsdate Date]] Map:^id (nsdate *value) {   NSLog (@ "value:%@", value);   Nsdatecomponents *datecomponents = [[Nscalendar Currentcalendar] components:nshourcalendarunit | Nsminutecalendarunit |  Nssecondcalendarunit Fromdate:value]; return [NSString stringwithformat:@ "%02ld:%02ld:%02ld", (Long) Datecomponents.hour, (Long) Datecomponents.minute, ( Long) Datecomponents.second];}];

  

Combination

RAC (self, submitbutton.enabled) = [Racsignal combinelatest:@[self.usernamefield.rac_textsignal, Self.passwordField.rac_textSignal] Reduce:^id (nsstring *username, NSString *password) {return @ (username.length >= 6 && password.length >= 6);}];

Bind the Usernamefield and Passwordfield signals together for reduce processing. Returns a bool value to bind with self.submitButton.enabled.


Network requests

Self.logincommand = [[Raccommand alloc] initwithsignalblock:^ (id sender) {    return [client LogIn];}];

[Self.loginCommand.executionSignals subscribenext:^ (racsignal *loginsignal) {
[Loginsignal subscribecompleted:^{
NSLog (@ "logged in successfully!");
}];
}];

  

Function-Responsive programming

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.