A graphic and text introduction summarizing the new features of RxJava 2.x
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
##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"); } });
Output:
onsubscribe start onNext--->0 onNext--->1 onNext--->2 ... onNext--->10 onComplete onsubscribe end
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);
Other observations Pattern
- Single/SingleObserver
- Completable/CompletableObserver
- Maybe/MaybeObserver
//判断是否登陆 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() { } });
event, you can use this observer pattern.
Action1——–ActionReference
#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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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.

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: 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 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

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
