现在RxJava已经是2.0以上版本了,看到很多资料上还在讲1.0的版本,因此做个简单的笔记,供大家参考,不足之处请兄弟们留言我添加完善
1、概述
RxJava(Reactive Extensions Java),RxJava基于观察者模式,是一种响应式编程模型,目的是提供统一的接口帮助开发者便捷的处理异步数据流。
RxJava本质上是一个异步库,使用简洁的逻辑处理繁琐复杂任务。
2、观察者模式
观察者模式涉及到2主角
1)Observable :被观察者
2)Observer:观察者
被观察者中有一个观察者的列表,当被观察者发生变化的时候回根据这张表一一通知观察者。
3、1.0和2.0的区别
3.1、背压
1.0背压集中在Obserable中处理,导致有点混乱,。
2.0版本把对背压的处理提出来了,出现了两种观察者模式:
Observable(被观察者)/Observer(观察者) 不支持背压(Backpressure)
Flowable(被观察者)/Subscriber(观察者) 支持背压(Backpressure)
Flowable是新增的
3.2、操作符修改
1.0版本
Action这类接口命名Action0、Action1…(数字代表可接受的参数),现在做出了改动
1.x
2.x
Action0
Action
Action1
Consumer
Action2
BiConsumer
...
ActionN
Funx修改
1.x
2.x
Func
Function
A functional interface that takes a value and returns another value
Func2
BiFunction
Func3 ~ Func9
Function3 ~ Function9
Function3
Function9
FuncN
Function
First()
1.x
2.x
first
firstElement
first(Func1)
filter(predicate).first()
firstOrDefault(T)
first(T)
firstOrDefault(Func1, T)
first(T)
3.3、线程调度 Schedulers
2.0去掉了Schedulers.immediate()、Schedulers.test()
3.4 、创建被观察者的变化
1.0版本
Observable observable = Observable.create(new Observable.OnSubscribe() {
@Override
public void call(Subscriber super String> subscriber) {
subscriber.onNext("hello");
subscriber.onNext("hello");
subscriber.onNext("hello");
subscriber.onCompleted();
}
});
2.0版本
变化点1:create参数Observable.OnSubscribe改为ObservableOnSubscribe
变化点2:回到函数call(Subscriber)改为 subscribe(ObservableEmitter)
变化点3:可发射三种事件:emitter.onNext(T value)、onComplete、onError(Throwable e)
Observable observable=Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter e) throws Exception {
e.onNext("hello!");
e.onNext("world!");
e.onNext("are you ok!");
e.onError(new Throwable("error"));
e.onComplete();
}
});
3.5、创建观察者的变化
1.0 中创建观察者Observer有两种方式
方式1:采用 Observer 接口
Observer observer = new Observer() {
@Override
public void onNext(String s) {
}
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
};
方式2:采用Subscriber接口创建, 其对 Observer接口进行了扩展,增加了onStart()、unSubscribe()
Subscriber subscriber = new Subscriber() {
@Override
public void onNext(String s) {
}
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
};
2.0 创建观察者Observer
变化点1:增加了回调方法onSubscribe(),其最先被调用,可用来做一些初始化的工作
变化点2:onCompleted()改成 onComplete()
Observer observer = new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Integer value) {
}
@Override
public void onError(Throwable e) {
}
// 注意
@Override
public void onComplete() {
}
}
3.6、接口都增加了异常抛出
public interface Action {
/**
* Runs the action and optionally throws a checked exception.
* @throws Exception if the implementation wishes to throw a checked exception
*/
void run() throws Exception;
}
public interface BiConsumer {
/**
* Performs an operation on the given values.
* @param t1 the first value
* @param t2 the second value
* @throws Exception on error
*/
void accept(T1 t1, T2 t2) throws Exception;
}
public interface Function {
/**
* Apply some calculation to the input value and return some other value.
* @param t the input value
* @return the output value
* @throws Exception on error
*/
R apply(T t) throws Exception;
}
/**
* A functional interface (callback) that accepts a single value.
* @param the value type
*/
public interface Consumer {
/**
* Consume the given value.
* @param t the value
* @throws Exception on error
*/
void accept(T t) throws Exception;
}
3.7 from 变化
from -> fromArray
1.0版本
private void doFrom() {
String[] items = {"item1", "item2", "item3"};
Observable.from(items)
.subscribe(new Action1() {
@Override
public void call(String s) {
System.out.println(s);
}
});
}
2.0版本
private void doFrom() {
String[] items = {"item1", "item2", "item3"};
Observable.fromArray(items).subscribe(new Consumer() {
@Override
public void accept(String s) throws Exception {
System.out.println(s);
}
});
}