Home Java javaTutorial A graphic and text introduction summarizing the new features of RxJava 2.x

A graphic and text introduction summarizing the new features of RxJava 2.x

Mar 31, 2017 am 10:55 AM

This article mainly introduces relevant information about the new features of RxJava 2.x. The article introduces it in detail through pictures, texts and sample codes. It has certain reference value for everyone. Friends who need it can take a look below.

What is RxJava

Rx is asynchronous reactive ProgrammingThe essence isObserver mode , asynchronous reactive programming with observers and subscribers.

This article mainly introduces relevant information about the new features of RxJava 2.x. I won’t say much below, let’s take a look at the detailed introduction.

Backpressure separation

A graphic and text introduction summarizing the new features of RxJava 2.x

##Flowable/Subscriber

Flowable.range(0,10)
.subscribe(new Subscriber<Integer>() {
 Subscription sub;
 //当订阅后,会首先调用这个方法,其实就相当于onStart(),
 //传入的Subscription s参数可以用于请求数据或者取消订阅
 @Override
 public void onSubscribe(Subscription s) {
 Log.w("TAG","onsubscribe start");
 sub=s;
 sub.request(1);
 Log.w("TAG","onsubscribe end");
 }
 @Override
 public void onNext(Integer o) {
 Log.w("TAG","onNext--->"+o);
 sub.request(1);
 }
 @Override
 public void onError(Throwable t) {
 t.printStackTrace();
 }
 @Override
 public void onComplete() {
 Log.w("TAG","onComplete");
 }
});
Copy after login

Output:

onsubscribe start
onNext--->0
onNext--->1
onNext--->2
...
onNext--->10
onComplete
onsubscribe end
Copy after login

As you can see from the output of the above code, when we call

subscription.request(n) method, the onNext method will be executed immediately before the code behind onSubscribe() is executed. Therefore, if you use a class that needs to be initialized in the onNext method, you should try to subscription.request(n) Do initialization work before calling this method;

Another creation method

Flowable.create(new FlowableOnSubscribe<Integer>() {
  @Override
  public void subscribe(FlowableEmitter<Integer> e) throws Exception {
  e.onNext(1);
  e.onNext(2);
  e.onNext(3);
  e.onNext(4);
  e.onComplete();
  }
 }
 //需要指定背压策略
 , BackpressureStrategy.BUFFER);
Copy after login

Other observations Pattern

  • Single/SingleObserver

  • Completable/CompletableObserver

  • Maybe/MaybeObserver


The above three are similar, more suitable for sending a single data, not suitable for sending a large amount of data.

//判断是否登陆
Maybe.just(isLogin())
 //可能涉及到IO操作,放在子线程
 .subscribeOn(Schedulers.newThread())
 //取回结果传到主线程
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(new MaybeObserver<Boolean>() {
  @Override
  public void onSubscribe(Disposable d) {
  }
  @Override
  public void onSuccess(Boolean value) {
  if(value){
   ...
  }else{
   ...
  }
  }
  @Override
  public void onError(Throwable e) {
  }
  @Override
  public void onComplete() {
  }
 });
Copy after login

In fact, this kind of observer The pattern is not used to send a large amount of data, but to send a single data. That is to say, when you only want the result (true or false) of a certain

event, you can use this observer pattern.

ActionRx1.0————–Rx2.0

Action1——–Action


Action1——– Consumer


Action2——-BiConsumer


The following Actions have been removed, only ActionN

Reference

A graphic and text introduction summarizing the new features of RxJava 2.x

A graphic and text introduction summarizing the new features of RxJava 2.x

A graphic and text introduction summarizing the new features of RxJava 2.x

A graphic and text introduction summarizing the new features of RxJava 2.x

A graphic and text introduction summarizing the new features of RxJava 2.x

A graphic and text introduction summarizing the new features of RxJava 2.x

A graphic and text introduction summarizing the new features of RxJava 2.x

A graphic and text introduction summarizing the new features of RxJava 2.x

#Summarize

The above is the detailed content of A graphic and text introduction summarizing the new features of RxJava 2.x. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.3 released: new features at a glance PHP 8.3 released: new features at a glance Nov 27, 2023 pm 12:52 PM

PHP8.3 released: Overview of new features As technology continues to develop and needs change, programming languages ​​are constantly updated and improved. As a scripting language widely used in web development, PHP has been constantly improving to provide developers with more powerful and efficient tools. The recently released PHP 8.3 version brings many long-awaited new features and improvements. Let’s take a look at an overview of these new features. Initialization of non-null properties In past versions of PHP, if a class property was not explicitly assigned a value, its value

A guide to learn the new features of PHP8 and gain an in-depth understanding of the latest technology A guide to learn the new features of PHP8 and gain an in-depth understanding of the latest technology Dec 23, 2023 pm 01:16 PM

An in-depth analysis of the new features of PHP8 to help you master the latest technology. As time goes by, the PHP programming language has been constantly evolving and improving. The recently released PHP8 version provides developers with many exciting new features and improvements, bringing more convenience and efficiency to our development work. In this article, we will analyze the new features of PHP8 in depth and provide specific code examples to help you better master these latest technologies. JIT compiler PHP8 introduces JIT (Just-In-Time) compilation

Using RxJava for asynchronous processing in Java API development Using RxJava for asynchronous processing in Java API development Jun 18, 2023 pm 06:40 PM

Java is a very popular programming language, especially widely used in web applications and mobile applications. When faced with some complex multi-threaded application development requirements, developers usually encounter many problems. RxJava is a very powerful library that provides asynchronous and event-based programming patterns based on the observer pattern. This article will introduce how to use RxJava for asynchronous processing in JavaAPI development. 1. What is RxJava? RxJava is a library based on the observer pattern

What are the new features of php8 What are the new features of php8 Sep 25, 2023 pm 01:34 PM

New features of php8 include JIT compiler, type deduction, named parameters, union types, properties, error handling improvements, asynchronous programming support, new standard library functions and anonymous class extensions. Detailed introduction: 1. JIT compiler, PHP8 introduces the JIT compiler, which is an important performance improvement. The JIT compiler can compile and optimize some high-frequency execution codes in real time, thereby improving the running speed; 2. Type derivation , PHP8 introduces the type inference function, allowing developers to automatically deduce the type of variables when declaring variables, etc.

An overview of the new features of CSS3: How to use CSS3 to achieve transition effects An overview of the new features of CSS3: How to use CSS3 to achieve transition effects Sep 09, 2023 am 11:27 AM

Overview of the new features of CSS3: How to use CSS3 to achieve transition effects CSS3 is the latest version of CSS. Among the many new features, the most interesting and practical one should be the transition effect. Transition effects can make our pages smoother and more beautiful during interaction, giving users a good visual experience. This article will introduce the basic usage of CSS3 transition effects, with corresponding code examples. transition-property attribute: Specify the CSS property transition effect that needs to be transitioned

Interpretation of new features of Go language: making programming more efficient Interpretation of new features of Go language: making programming more efficient Mar 10, 2024 pm 12:27 PM

[Interpretation of new features of Go language: To make programming more efficient, specific code examples are needed] In recent years, Go language has attracted much attention in the field of software development, and its simple and efficient design concept has attracted more and more developers. As a statically typed programming language, Go language continues to introduce new features to improve development efficiency and simplify the code writing process. This article will provide an in-depth explanation of the latest features of the Go language and discuss how to experience the convenience brought by these new features through specific code examples. Modular development (GoModules) Go language from 1

Overview of the new features of CSS3: How to use CSS3 to achieve horizontally centered layout Overview of the new features of CSS3: How to use CSS3 to achieve horizontally centered layout Sep 09, 2023 pm 04:09 PM

Overview of the new features of CSS3: How to use CSS3 to achieve horizontally centered layout In web design and layout, horizontally centered layout is a common requirement. In the past, we often used complex JavaScript or CSS tricks to achieve this. However, CSS3 introduced some new features that make horizontally centered layouts simpler and more flexible. This article will introduce some new features of CSS3 and provide some code examples to demonstrate how to use CSS3 to achieve horizontally centered layout. 1. Use flexbox to layout fle

New Redis extension introduced in PHP8.1 New Redis extension introduced in PHP8.1 Jul 07, 2023 pm 09:41 PM

The new Redis extension introduced in PHP8.1 With the rapid development of the Internet, a large amount of data needs to be stored and processed. In order to improve the efficiency and performance of data processing, caching has become an indispensable part. In PHP development, Redis, as a high-performance key-value storage system, is widely used in caching and data storage scenarios. In order to further improve the experience of using Redis in PHP, PHP8.1 introduces a new Redis extension. This article will introduce the new functions of this extension and provide

See all articles