Rxjava Series 1 (Introduction)

Source: Internet
Author: User
Tags java web

Objective

Improving development efficiency and reducing maintenance costs have always been a constant tenet of the development team. Nearly a year in the domestic technical circles more and more began to mention the RX, after a period of study and exploration I also deeply felt the charm of Rxjava. It can help us simplify code logic and improve code readability. This is great for the improvement of development efficiency and the reduction of later maintenance costs. Personal prediction Rxjava must be a major trend in the 2016, so there are plans to introduce it into the company's existing projects, writing this series of articles is mainly for the team to do technical sharing.

Since I am an Android ape, the scenes in this series of articles are based on the Android platform. If you're a Java Web engineer or something else, that's okay, I'll try to describe the problem in plain language.

Responsive programming

Before we introduce Rxjava, we'll talk about responsive programming. So what is responsive programming? Responsive programming is a programming model based on the concept of asynchronous data flow. Data flow is like a river: it can be observed, filtered, manipulated, or merged into a new stream for new consumers with another stream.

One of the key concepts of responsive programming is events. Events can be waited for, can trigger a process, and can trigger other events. Events are the only appropriate way to map our real world to our software: if it's too hot in the house, we'll open a window. Similarly, when our weather app gets new weather data from the server, we need to update the UI that displays the weather information on the app, and the lane-shifting system on the car detects that the vehicle is offset from the normal route and reminds the driver to correct, or respond to, the event.

Today, one of the most common scenarios for responsive programming is the UI: our mobile app must respond to network calls, user touch input, and system bullets. In this world, software is event-driven and responsive because real life is the same.

Some of the concepts in this section are excerpted from the book "RxJava Essentials"

The origins of Rxjava

Rx is a responsive extension of Microsoft. NET, and Rx provides an easy way to create asynchronous, event-driven programs with observable sequences. 2012 Netflix began migrating. NET Rx to the JVM in response to growing business needs. The Rxjava was formally displayed in February 13.
From a semantic point of view, Rxjava is. NET Rx. From a grammatical point of view, Netflix takes into account the corresponding Rx method, preserving Java code specifications and basic patterns.

Rxjava Origin what is Rxjava

So what exactly is Rxjava? I define it as: Rxjava isessentially an asynchronous operations library, an asynchronous event library that allows you to handle tedious and complex tasks with extremely concise logic.

Rxjava fortunately

Why do we have to choose Rxjava when the Asynctask,handler is provided by the Android platform as a class library for asynchronous operations? The answer is simple! Rxjava can use very concise code logic to solve complex problems, and even if the business logic is more and more complex, it can remain concise! In conjunction with lambda, use a few simple lines of code to solve the business problem you are responsible for in minutes. Jane is pushing the table, and it's great to put it on!

Say more useless, on the code!

Suppose we have a demand on the user app, need to pull from the service end of the Shanghai Pudong New Area Tangqiao plate community[] communities, each cell contains multiple sets of listings listThis example is derived from the Rxjava of the line to the Android developer. )

New Thread () {@Overridepublic void run  () {super.run (); //get cell list from server list<community> communities = Getcommunitiesfromserver (); for (Community community:communities) {listfor (House house:houses) {if (house.price >= 5000000 {Runonuithread (new Runnable () { @Override public void run< Span class= "Hljs-params" > () {//Add the information of the House to the screen addhouseinformationtoscreen (houses);}); }}}}}.start ();               

The way to use Rxjava is this:

Observable.from (Getcommunitiesfromserver ()). FlatMap (New func1<community, observable@OverridePublic observablePager(Community Community) {return observable.from (community.houses);} }). Filter (new Func1 @Override public Boolean call return house.price>= 5000000;} }). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()). Subscribe ( new action1 @Override public void call (house houses) {//add information to the screen Addhouseinformationtoscreen (house); } }); 

From the above code we can see: Although the code volume seems to be complicated, but the implementation of the Rxjava is a chain call, without any nesting, the entire implementation logic looks exceptionally concise and clear, which is of great help to our programming implementation and post-maintenance. Especially for those scenarios where callbacks are nested. With lambda expressions You can also simplify this:

  Observable.from(getCommunitiesFromServer())            .flatMap(community -> Observable.from(community.houses))            .filter(house -> house.price>=5000000).subscribeOn(Schedulers.io())            .observeOn(AndroidSchedulers.mainThread())            .subscribe(this::addHouseInformationToScreen);

Simple! There is beauty! This is the code that a feeling programmer should write.

After reading this article you should be able to understand why Rxjava is getting more and more hot. It can greatly improve the efficiency of our development and the readability of the code! Of course, Rxjava learning curve is also relatively steep, in the following article I will be the main points of knowledge to do a detailed introduction, please pay attention!




Links: https://www.jianshu.com/p/ec9849f2e510

Rxjava Series 1 (Introduction)

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.