Rxjava Development Essentials 1-from. NET to Rxjava

Source: Internet
Author: User
Tags iterable

  • The original is from "RxJava Essentials"
  • Original Author: Ivan Morgillo
  • Development technology Front www.devtf.cn
  • Reprint statement: This translation has authorized developers to enjoy the exclusive right to reproduce the headlines, without permission, not reproduced!
  • Translator: Yuxingxin
  • Project Address: RXJAVA-ESSENTIALS-CN
RX-from. NET to Rxjava

Responsive programming is a programming model based on the concept of asynchronous data flow. Data flow is like a river: it can be measured, filtered, manipulated. or merge the new consumer with another stream into a new stream.

One of the key concepts of responsive programming is events. Events can be waited on. To trigger the process. can also trigger other events. Events are the only appropriate way to map our real world to our software: Let's say we open a window if it's too hot in the house. The same. When we change some of the values in the electronic watch (change propagation), we need to update the entire table or our robot will turn when it touches the wall (responding to events).

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 to real-life.

Microsoft Responsive Extensions

Function-responsive programming is a revelation from Erik Meijer, a computer scientist in the late 90, by Microsoft. Used to design and develop the RX library for Microsoft.

Rx is Microsoft. NET is a responsive extension.

RX provides an easy way to create asynchronous, event-driven programs with observable sequences. Developers can use observables to emulate asynchronous data streams and query observables using LINQ syntax. And very easy to manage the concurrency of the scheduler.

Rx makes well-known concepts easier to implement and consume, such as the push method . In a responsive world. We can not pretend that the user is not concerned about or is not complaining about it and blindly wait for the return result of the function, the network call, or the return result of the database query.

We are always waiting for something, which allows us to lose the chance to handle other things in parallel, to provide a better user experience, to keep our software from being affected by sequential chains, and to block programming.

The. NET Observable associated with the. NET enumeration are listed in the following table.

. NET Observable a return value Multiple return Values
Pull/synchronous/interactive T IEnumerable<T>
Push/asynchronous/reactive Task<T> IObservable<T>

The push method reverses the problem: instead of waiting for the result, the developer is simply the result of the request, and when it returns, a notification is available. Developers provide a clear chain of response to upcoming events. For each event, the developer responds with a corresponding response; When the user is asked to log in, submit a form that carries his username and password. The application runs the logon network request, and the next thing that will happen is:

    • Displays a successful message and saves the user's personal information.
    • Display an incorrect message

As you can see with the push method. Developers do not need to wait for results. Instead, notify him when the results are returned. During this time, he was able to do whatever he wanted to do:

    • Displays a progress dialog box
    • Save username and password for the next login
    • Preload some of the things that he feels are time-consuming to log in after success
Coming to the Java world-Netflix RxJava

In 2012, Netflix began to realize that their architecture was struggling to meet their huge user base. So they decided to design the architecture again to reduce the number of rest calls.

Instead of the rest call dozens of times. Instead, let the client handle the required data itself. They decided to create a specially optimized rest call based on client requirements.

In order to achieve this goal. They decided to try the response and start migrating. NET Rx to the JVM. They don't want to be based solely on the Java language. But the entire JVM. This makes it possible to provide a new tool for every JVM-based language on the market: Java, Closure, Groovy, Scala, and so on.

In February 2013, Ben Christensen and Jafar Husain an article on the Netflix Technology blog for the first time to show the world Rxjava.

The main features are:

    • Easy concurrency to make better use of server capabilities.
    • Easy to run asynchronously with conditions.
    • A better way to avoid callbacks to hell.
    • A reactive approach.

As the. Net,rxjava Observable is the equivalent of the push iteration. That is pull. The Pull method is a blocking and waiting method: The consumer pull value from the source. and plug the thread until the producer provides a new value.

The push method is used for subscriptions and responses: Consumers subscribe to the launch of new values, and when they are available, the producer push these new values and notifies the consumer. At this point, consumers consume them. The push method is significantly more flexible. From a logical and practical point of view, a developer simply needs to ignore whether the data he needs is from synchronous or asynchronous, and his code will still work.

The difference between Rxjava

From a pure Java perspective, the RxJava observable class derives from the classic gang of four observer patterns.

It adds three missing features:

    • The producer can signal notification when there are not many other data available: the oncompleted () event.
    • The producer can signal notification when an error occurs: the OnError () event.
    • RxJava observables can be combined rather than nested, thus preventing developers from falling into a callback hell.

Observables and Iterables share a similar API: Many of the operations we can run in iterable are the same and can be run on observables. Of course, due to the nature of the observables stream. There are no corresponding methods such as Iterable.remove ().

Pattern a return value Multiple return Values
Synchronous T getData() Iterable<T>
Asynchronous Future<T> getData() Observable<T> getData()

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 the Java code specification and the main pattern.

Summarize

In this chapter, we initially explored the world of responsiveness. From Microsoft's. NET to Netflix's Rxjava. We understand how Rx was born, and we also learned that traditional methods are similar and different from reactive methods.

Next chapter. We will learn what observables is and how to create it and apply responsive programming to our daily coding.

Rxjava Development Essentials 1-from. NET to Rxjava

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.