ReactiveCocoa getting started-Part 2), reactivecocoa tutorial

Source: Internet
Author: User

Getting started with ReactiveCocoa-Part 2 (), reactivecocoa tutorial

ReactiveCocoaIs a framework that allows you to use in iOS apps.Function responsive programming(FRP) technology. In this series of tutorialsPart 1You learned how to replace the standard action and event processing logic with the signal for sending the event stream. You also learned how to convert, split, and aggregate these signals.

In the second part of this series of tutorials, you will learn some advanced features of ReactiveCocoa, including:

  • Two other event types:ErrorAndCompleted
  • Throttling
  • Thread
  • Extension
  • Others

It's time to study it in depth.

Twitter Instant

In this tutorial, the app you want to develop is Twitter Instant (based onGoogle InstantThis application can search for the content on Twitter and update the search results in real time based on the input.

This applicationInitial ProjectIncludes some basic UI and required code. AndPart 1You need to useCocoaPodsTo obtain the ReactiveCocoa framework and integrate it into the project. The initial project already contains a required Podfile. Open the terminal and run the following command:

pod install

If the execution is correct, you can see the output similar to the following:

Analyzing dependencies Downloading dependencies Using ReactiveCocoa (2.1.8) Generating Pods project Integrating client project

This generates an Xcode workspcae,TwitterInstant. xcworkspace. Open it in Xcode and confirm that it contains two projects:

  • TwitterInstant: The application logic is here.
  • Pods: Here is the external dependency. Currently, only ReactiveCocoa is included.

Build and run, you can see the following interface:

Take some time to familiarize yourself with the application code. This is a simple application based on the split view controller. The left column isRWSearchFormViewControllerIt adds some UI controls through storyboard and connects the search text field through outlet. The right column isRWSearchResultsViewControllerIs only a subclass of UITableViewController.

OpenRWSearchFormViewController. mIn the viewDidLoad method, first locate the results view controller and assign it to the private attribute of resultsViewController. The main logic of the application will be concentrated inRWSearchFormViewControllerThis attribute provides the search resultRWSearchResultsViewController.

 

Verify the validity of the Search Text

The first thing to do is to verify the search text to ensure that the text length is greater than 2 characters. If you have completed the first part of this series of tutorials, you should be familiar with this.

InRWSearchFormViewController. mAdd the following method under viewDidLoad in:

- (BOOL)isValidSearchText:(NSString *)text {    return text.length > 2;}

This method only ensures that the length of the string to be searched is greater than 2 characters. This logic is very simple. You may ask "why do you want to write such a separate method in the project file ?".

Currently, the logic for verifying input validity is simple, but what if the logic needs to become more complex in the future? If it is the same as in the above example, you only need to modify one place. The Code itself shows why you want to check the length of a string.

At the top of RWSearchFormViewController. m, introduce ReactiveCocoa:

#import <ReactiveCocoa.h>

Add the following code to the bottom of viewDidLoad:

[[self.searchText.rac_textSignal     map:^id(NSString *text) {        return [self isValidSearchText:text] ?             [UIColor whiteColor] : [UIColor yellowColor];    }]     subscribeNext:^(UIColor *color) {         self.searchText.backgroundColor = color; }];

What did the above Code do?

  • Obtain the text signal of the search text field
  • Convert it to color to indicate whether the input is valid
  • Then apply the color in subscribeNext: block to the backgroundColor attribute of the search text field.

Build and run. If the input text is too short, the background of the text field turns yellow to indicate that the input is invalid.

The process is similar to the following:

When the text in text field changes, rac_textSignal sendsNextEvent. The event contains the text in the current text field. Map converts the text value to the color value, so subscribeNext: This step obtains the color value and applies it to the background color of the text field.

You should remember this series of tutorialsPart 1? If you forget it, we recommend that you stop here and check the first part.

Before adding the Twitter search logic, there are some interesting topics to talk about.

Memory Management

Let's see what you addTwitterInstantAre you curious about how these pipelines are held? Obviously, it is not assigned to a variable or attribute, so it will not increase the reference count. How can it be destroyed?

One goal of the ReactiveCocoa design is to support the programming style of generating pipelines anonymously. So far, this should be intuitive among all the responsive code you have written.

To support this model, ReactiveCocoa holds all global signals. If a signal has one or more subscribers, the signal is active. If all subscribers are removed, the signal will be destroyed.

Related Article

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.