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 reprint, 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 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 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 because real life is the same.

Microsoft Responsive Extensions

Function-responsive programming is a thought inspired by a Microsoft computer scientist Erik Meijer in the late 90 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, query observables using LINQ syntax, and easily 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't pretend that the user is not paying attention or complaining about it and waiting 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 simply requests the result and gets a notification when it returns. The developer provides a clear response chain for upcoming events. For each event, the developer responds accordingly; For example, when a user is asked to log in, submit a form that carries his user name and password. The application executes 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 don't have to wait for results. Instead, notify him when the results are returned. During this time, he can do whatever he wants to do:

    • Displays a progress dialog box
    • Save the user name and password for the next logon
    • Preload some of the things he thinks will take time to process after a successful login
Coming to the Java world-Netflix RxJava

Netflix began to realize in 2012 that their architecture was struggling to meet their huge user base. So they decided to redesign the architecture to reduce the number of rest calls. Instead of dozens of rest calls, the client processes the required data themselves, and they decide to create a specially optimized rest call based on the client's requirements.

To achieve this, they decided to try a reactive style and start migrating. NET Rx to the JVM. They don't want to be based only on the Java language, but the entire JVM, which is likely to provide a new tool for every JVM-based language in 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 better utilize the server's capabilities.
    • Easy and conditional asynchronous execution.
    • 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 the value from the source and blocks 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 obviously more flexible because, from a logical and practical point of view, the developer simply ignores whether the data he needs is synchronous or asynchronous; 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:

    • Producers can signal when no more data is 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 getting into a callback hell.

Observables and Iterables share a similar API: Many of the operations we can perform in iterable can also be performed on observables. Of course, due to the nature of the observables flow, there is no corresponding method 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 Java code specifications and basic patterns.

Summarize

In this chapter, we initially explored the world of responsiveness. From Microsoft's. NET to Netflix Rxjava, we learned how Rx was born, and we also learned about the similarities and differences between traditional methods and reactive methods.

In the 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.