I. Basic concepts
Observable: Launch source, becoming "observee" in observer mode
Observer: Receiving source, "observer" can receive data transmitted by Observable Subject
Subject: Subject is a special object, which can be either a transmitter or a receiver.
Subscriber: "Subscriber" implements the Observer interface, which is one of the most important methods for unsubscribe(); for unsubscriber cancellation, it is recommended that Subscriber be used as the recipient.
Subscription: Observable calls the object returned by the subscribe() method, which can also cancel the subscription.
Action0: An interface in RxJava. It only has a call() method with no parameters and no return value. It also has Action1,... Action9 et al. Action1 encapsulates a call() method with one parameter, call (T), and so on.
Func0: Similar to Action0, there are call () methods, but with return values
2. Brief introduction of the use process:
Create a data emitter for transmitting data:
Observable<String> sender = Observable.create(new Observable.OnSubscrible<String>(){
@Override
public void call(Subscriber<? super String> subscriber){
subscriber.onNext("hello world")
}
})
Create a data receiving source
Observer<String> receiver = new Observer<String>() {
@Override
public void onCompleted() {
//Call upon completion of data reception
}
@Override
public void onError(Throwable e) {
//Error call occurs
}
@Override
public void onNext(String s) {
//Normal Received Data Call
System.out.print(s); //Will receive greetings from sender: "Hi, Weavey! "
}
};
sender.subscriber(receiver) links the observer to the observer. send launches "hello world" which will be received by reveiver. This is the simplest process of use. Rxjava allows asynchrony, and the observer mode becomes simpler.
The Use of Rxjava in Project
The creation of Observable:
1. Use create():
normalObserable = Observable.create(new Observable.OnSubcribe<String>(){
@Override
public void call(Subscriber<? super String> subscriber) {
subscriber.onNext("create1"); //Launch a "create1" String
subscriber.onNext("create2"); //Launch a "create 2" String
subscriber.onCompleted();//Launch is complete. This method calls onCompleted manually before it calls back Observer's onCompleted method
}})
2. Use just(); create an Observable for you and automatically call onNext() to send data for you:
justObservable = Observable.just("just1","just2");//Send in turn"just1","just2";
3. Use from(), traverse the collection and send each item:
List<String> list = new ArrayList();
list.add("from1");
list.add("from2");
list.add("from3");
fromObservable = Observabel.from(list);//Traversing through the list, sending one at a time, - - just method can also pass the list, but sending the entire list object, from is sending an item of the list.
4. Using defer(), Observable is created only when an observer subscribes, and a new Observable is created for each observer.
deferObservable = Observable.defer(new Func0<Observable<String>()>{
@Override
//Note that the call method here has no Subscriber parameter
public Observable<String> call() {
return Observable.just("deferObservable");
})
5. Using interval(), create an Observable that transmits Integer Sequences at fixed intervals, which can be used as a timer.
intervalObservable = Observable.interval(1,TimeUnit.SECONDS);//Send every other second
6. Use range() to create an Observable that transmits a specific sequence of integers. The first parameter is the starting value, the second is the number of transmitted integers. If it is 0, it does not send, and the negative number throws an exception.
rangeObservable = Observable.range(10,5)//Integers 10, 11, 12, 13, 14 will be sent
7. Using Timer(), create an Observable that emits a special value after a given delay, equivalent to handler.postDelay();
timeObservable = Observabel.timer(3,TimeUnit.SECONDS); //Send a value in three seconds
8. Using repeat(), create an Observable that repeatedly transmits specific data
repeatObservable = Observable.just("repeatObservable").repeat(3);//Repeated launch three times
Creation of Observer
mObserver = new Observer<String>() {
@Override
public void onCompleted() {
LogUtil.log("onCompleted");
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
LogUtil.log(s);
}};
With Observable and Observer, it's easy to do this. Let's take a created Observable and Observer association to form an example of RxJava.
justObservable.subscribe(mObserver);
mObserver's onNext() method receives data from justObservable in turn "just1", "just2". In addition, if you don't care whether the data has been received or whether there are errors, you don't need onCompleted and onError methods. Action1,subscribe() can be used to support Action1 as a parameter, and Rxjava will call his call method to receive data.
justObservable.subscribe(new Action1<String>(){
@Override
public void call(String s) {
LogUtil.log(s);
}})
This is the simple use of RxJava. The reason why RxJava is popular with users is that with the complexity of logic and the change of requirements, the code can still maintain a strong readability.
have access to filter Adding restrictions
Observable.create(new Observable.OnSubscribe<List<User>>() {
@Override
public void call(Subscriber<? super List<User>> subscriber) {
List<User> userList = null;
···
//Get user table data from the database and assign it to userList
···
subscriber.onNext(userList);
}
}).flatMap(new Func1<List<User>, Observable<User>>() {
@Override
public Observable<User> call(List<User> users) {
return Observable.from(users);
}
}).filter(new Func1<User, Boolean>() {
@Override
public Boolean call(User user) {
return user.getName().equals("Xiao Ming");
}
}).subscribe(new Action1<User>() {
@Override
public void call(User user) {
//Get Xiao Ming's data
}
});