create
The create operator is the “root” of all create operators, which means that all other create operators end up creating observables from the create operator
An example call is as follows:
Observable.create(new Observable.OnSubscribe() {
@Override
public void call(Subscriber observer) {
try {
if (!observer.isUnsubscribed()) {
for (int i = 1; i < 5; i++) {
observer.onNext(i);
}
observer.onCompleted();
}
} catch (Exception e) {
observer.onError(e);
}
}
} ).subscribe(new Subscriber() {
@Override
public void onNext(Integer item) {
System.out.println("Next: " + item);
}
@Overridepublicvoid onError(Throwable error) {
System.err.println("Error: " + error.getMessage());
}
@Overridepublicvoid onCompleted() {
System.out.println("Sequence complete.");
}
});Copy the code
The running results are as follows:
Next: 1
Next: 2
Next: 3
Next: 4
Sequence complete.
Copy the code
from
The FROM operator converts other types of objects and data types into Observables
An example call is as follows:
Integer[] items = { 0, 1, 2, 3, 4, 5 }; Observable myObservable = Observable.from(items); myObservable.subscribe( new Action1() { @Override publicvoid call(Integer item) { System.out.println(item); } }, new Action1() { @Override publicvoid call(Throwable error) { System.out.println("Error encountered: " + error.getMessage()); } }, new Action0() { @Override publicvoid call() { System.out.println("Sequence complete"); }});Copy the code
The running results are as follows:
0
1
2
3
4
5
Sequence complete
Copy the code
just
The just operator converts other types of objects and data types into Observables, much like the FROM operator, except that the method parameters are different
An example call is as follows:
Observable.just(1, 2, 3) .subscribe(new Subscriber() { @Overridepublicvoid onNext(Integer item) { System.out.println("Next: " + item); } @Overridepublicvoid onError(Throwable error) { System.err.println("Error: " + error.getMessage()); } @Overridepublicvoid onCompleted() { System.out.println("Sequence complete."); }});Copy the code
The running results are as follows:
Next: 1
Next: 2
Next: 3
Sequence complete.
Copy the code
defer
The defer operator, which creates and executes an Observable from its factory methods until a subscriber subscribes, keeps the status of the Observable up to date
Let’s compare the results of running the defer and just operators
Integer i = 10; @SuppressWarnings("unchecked") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Observable justObservable = Observable.just(i); i = 12; Observable deferObservable = Observable .defer(new Func0>() { @Override public Observable call() { return Observable.just(i); }}); i = 15; justObservable.subscribe(new Subscriber() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Object o) { System.out.println("just result:" + o.toString()); }}); deferObservable.subscribe(new Subscriber() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Object o) { System.out.println("defer result:" + o.toString()); }}); }Copy the code
The running results are as follows:
just result:10
defer result:15
Copy the code
As you can see, the just operator does the assignment as soon as it creates an Observable, while defer creates an Observable when the subscriber subscribes, which is when the actual assignment takes place
Note: cannot use int
timer
The timer operator creates a sequence of numbers that are generated at a fixed interval; There are two cases:
There's one that produces a number every once in a while and then it ends, which is kind of a delayed number generation and there's one that produces a number every once in a while and there's no terminator, which is an infinite number of consecutive numbersCopy the code
The timer operator runs on a new thread by default, although you can change the thread on which it runs by passing in parameters.
Here is an example call:
Subscription observable = null; @SuppressWarnings({ "unchecked", "deprecation" }) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); observable = Observable.timer(2, 2, TimeUnit.SECONDS) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber() { @Override public void onCompleted() { // TODO Auto-generated method stub } @Override public void onError(Throwable arg0) { // TODO Auto-generated method stub } @Override public void onNext(Long arg0) { // TODO Auto-generated method stub System.out.println("Next:" + arg0.toString()); }}); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); if (observable ! = null) { observable.unsubscribe(); }}Copy the code
The running results are as follows:
Next:0 Next:1 Next:2 Next:3......Copy the code
Note that the observable.subscribe () method returns a Subscription object, which is returned every time we subscribe, and can be used to unsubscribe.
interval
The interval operator produces a number at regular intervals, starting at 0 and incrementing by one to infinity; The interval operator performs the same as the second case of the timer operator above.
Subscription observable = null; @SuppressWarnings({ "unchecked", "deprecation" }) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); observable = Observable.interval(2, TimeUnit.SECONDS) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber() { @Override public void onCompleted() { // TODO Auto-generated method stub } @Override public void onError(Throwable arg0) { // TODO Auto-generated method stub } @Override public void onNext(Long arg0) { // TODO Auto-generated method stub System.out.println("Next:" + arg0.toString()); }}); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); if (observable ! = null) { observable.unsubscribe(); }}Copy the code
Running results:
It keeps increasing unless unsubscribe is called.
range
The range operator creates a sequence of m numbers starting at n, such as range(3,10), which creates 3, 4, 5… The group of numbers 12
An example call is as follows:
Subscription observable = null; @SuppressWarnings({ "unchecked", "deprecation" }) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); observable = Observable.range(0, 10).subscribe(new Subscriber() { @Override public void onCompleted() { // TODO Auto-generated method stub System.out.println("Sequence complete."); } @Override public void onError(Throwable arg0) { // TODO Auto-generated method stub } @Override public void onNext(Integer arg0) { // TODO Auto-generated method stub System.out.println(""+arg0); }}); }Copy the code
Running results:
repeat/repeatWhen
The repeat operator returns multiple results to an Observable
The repeatWhen operator conditionally resubscribes to an Observable to produce multiple results
Repeat and repeatWhen operators run on a new thread by default, although you can change the thread on which they run by passing in arguments.
An example of a repeat call is as follows:
// generate two groups (1,2,3) of digital Subscription observable = null; @SuppressWarnings({ "unchecked", "deprecation" }) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); observable = Observable.range(1, 3).repeat(2).subscribe(new Subscriber() { @Override public void onCompleted() { // TODO Auto-generated method stub System.out.println("onCompleted"); } @Override public void onError(Throwable arg0) { // TODO Auto-generated method stub } @Override public void onNext(Integer arg0) { // TODO Auto-generated method stub System.out.println(""+arg0); }}); }Copy the code
The running results are as follows:
Say something
I have to say that the Rx operators are too powerful and, of course, too large to remember them all, at least not for me.
Reactivex. IO /documentati… reactivex. IO /documentati…
Well, English is not good, also not panic, look at this, after all, most programmers English… . (do not include me) McXiaoke. Gitbooks. IO/rxdocs/cont…
These are all detailed batch.
The last article in this series on RxJava is also about Rx operators, which I have also written in a Demo. I won’t say.
For more operators, see the next decomposition