operator Type
- Create action
- Transform operations
- Filtering operations
- Combine operations
- Error handling
- Secondary operations
- Conditions and Boolean operations
- Arithmetic and aggregation operations
- Connection operation
- Conversion operations
Create action
create
You can create a observable from scratch using the Create operator, pass a function that takes the observer as a parameter, write this function to behave as a onnext that observable– the proper invocation of the observer, OnError and OnCompleted methods.
A properly formed finite observable must attempt to invoke the Observer's oncompleted exactly once, or its onerror exactly once, and then no longer invoke any other method of the observer.
The Create operator is the "root" of all created operators, which means that the other created operators are finally created with the Create operator, with the following flowchart examples: observable
Sample Code
Observable.create (NewObservable.onsubscribe<integer> () {@Override Public void Pager(SUBSCRIBER<?SuperInteger> observer) {Try{if(!observer.isunsubscribed ()) { for(inti =1; I <5; i++) {observer.onnext (i); } observer.oncompleted (); } }Catch(Exception e) {Observer.onerror (e); }}). Subscribe (NewSubscriber<integer> () {@Override Public void OnNext(Integer Item) {System.out.println ("Next:"+ Item); }@Override Public void OnError(Throwable error) {System.err.println ("Error:"+ error.getmessage ()); }@Override Public void oncompleted() {System.out.println ("Sequence complete."); } });
The results of the operation are as follows:
Next:1
Next:2
Next:3
Next:4
Sequence complete.
When using the Create operator, it is best to increase the isunsubscribed judgment in the call function of the callback so that when subscriber cancels the subscription no longer executes the associated code logic in the call function, thus avoiding some unexpected errors.
from
Will other types (confined to future, iterable and arrays only)? ) to convert the object and data type to observable.
The From operator can convert future, iterable, and array. For iterable and arrays, the resulting observable will emit iterable or array of each item of data. For the future, it emits a single data returned by the Future.get () method. The From method has a version that accepts two optional parameters, specifying the timeout length and time units, respectively. If the specified length of time has not returned a value, the observable will emit an error notification and terminate.
The from default is not performed on any particular scheduler. You can, however, pass the scheduler as an optional second parameter to observable, which will manage the future on that scheduler.
- From (Array)
- From (iterable)
- From (future)
- From (Future,scheduler)
- From (Future,timeout, Timeunit)
The flowchart example is as follows:
Sample Code
integer[] items = {0,1,2,3,4,5};observable myobservable = Observable. from(items); Myobservable.subscribe (NewAction1<integer> () {@Override Public void Pager(Integer Item) {System. out. println (item); } },NewAction1<throwable> () {@Override Public void Pager(Throwable error) {System. out. println ("Error encountered:"+ error.getmessage ()); } },NewAction0 () {@Override Public void Pager() {System. out. println ("Sequence Complete"); } });
The results of the operation are as follows:
0
1
2
3
4
5
Sequence Complete
just
Just converts a single data into a observable that emits that data.
Just is similar to from
, but will from
take out the iterable of an array or a character and emit it one by one, while just is simply emitted as an array or iterable as a single data.
Note: If you pass null to just, it returns a observable that emits a null value. Do not mistakenly think that it will return an empty observable (observable without transmitting any data at all), if you need empty observable you should use the empty operator.
It accepts one to nine parameters and returns a observable that emits the data in the order of the parameter list.
The flowchart example is as follows:
Sample Code
Observable.just (1,2,3). Subscribe (NewSubscriber<integer> () {@Override Public void OnNext(Integer Item) {System.out.println ("Next:"+ Item); }@Override Public void OnError(Throwable error) {System.err.println ("Error:"+ error.getmessage ()); }@Override Public void oncompleted() {System.out.println ("Sequence complete."); } });
The results of the operation are as follows:
Next:1
Next:2
Next:3
Sequence complete.
defer
The defer operator waits until an observer subscribes to it, and then it uses the observable factory method to generate a observable. It does this for every observer, so even though each subscriber thinks they are subscribing to the same observable, in fact each subscriber gets their own separate data series.
The defer operator ensures that the observable state is up-to-date.
This operator takes a observable factory function of your choice as a single parameter. This function has no arguments and returns a observable.
The defer method is not executed by default on any particular scheduler.
The flowchart example is as follows:
defer operator and just operator run result comparison sample program
I=Ten; Observable justobservable = Observable.just (i); I= A; Observable deferobservable = Observable.defer (NewFunc0<observable<object>> () {@Override PublicObservable<object>Pager() {returnObservable.just (i); } }); I= the; Justobservable.subscribe (NewSubscriber () {@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 (NewSubscriber () {@Override Public void oncompleted() { }@Override Public void OnError(Throwable e) { }@Override Public void OnNext(Object o) {System.out.println ("defer result:"+ o.tostring ()); } });
The results of the operation are as follows:
Just Result:10
Defer result:15
As you can see, the just operator does the assignment when the observable is created, and defer creates the observable when the Subscriber subscribes, so that the actual assignment is performed.
timer
Note: This operator is not recommended because an operator named interval has the same function.
The timer operator creates a observable that sends a number periodically, and here are two things:
creates a observable that emits a special value after a given delay
The timer returns a observable that emits a simple number 0 after delaying a given amount of time.
The timer operator is executed by default on the computation scheduler. There is one variant that can specify scheduler with optional parameters.
- Timer (Long,timeunit)
- Timer (Long,timeunit,scheduler)
The process examples are as follows:
Create a observable that produces a number every once in a while without a terminator, that is, an infinite number of consecutive digits.
- Timer (Long,long,timeunit)
The process examples are as follows:
Sample Program
//Every two seconds generates a numberObservable.timer (2,2, timeunit.seconds). Subscribe (NewSubscriber<long> () {@Override Public void oncompleted() {System. out. println ("Sequence complete."); } @Override Public void OnError(Throwable e) {System. out. println ("Error:"+ e.getmessage ()); } @Override Public void OnNext(Long along) {System. out. println ("Next:"+ along.tostring ()); } });
The results of the operation are as follows:
next:0
Next:1
Next:2
Next:3
......
interval
Creates a observable that emits an integer sequence at regular intervals, which emits an infinitely incrementing sequence of integers at regular intervals. These numbers start at 0 and increment by 1 at a time until infinity.
- Interval (long,timeunit)
- Interval (Long,timeunit,scheduler)
- Interval (long,long,timeunit))
- Interval (Long,long,timeunit,scheduler)
The interval is executed by default on the computation scheduler. You can also pass an optional scheduler parameter to specify the scheduler.
The process examples are as follows:
Example program: The interval operator is implemented as the second case of the timer operator above.
range
Creates a sequence of observable that emit a sequence of specific integers, which emit a series of ordered integers within a range, and you can specify the start and length of the range.
It accepts two parameters, one is the starting value of the range, and the other is the number of data in the range. If you set the second parameter to 0, the observable will not emit any data (if set to a negative number, it will throw an exception).
Range is not executed on any particular scheduler by default. There is one variant that can specify scheduler with optional parameters.
- Range (Int,int)
- Range (Int,int,scheduler)
The process examples are as follows:
Sample Program
//Generate consecutive numbers starting from 3 with a number of 10Observable.range (3,Ten). Subscribe (NewSubscriber<integer> () {@Override Public void oncompleted() {System. out. println ("Sequence complete."); } @Override Public void OnError(Throwable e) {System. out. println ("Error:"+ e.getmessage ()); } @Override Public void OnNext(Integer i) {System. out. println ("Next:"+ i.tostring ()); } });
The results of the operation are as follows:
Next:3
Next:4
Next:5
Next:6
....
Next:12
Sequence complete.
repeat/repeatWhen
repeat
Repeat transmit data repeatedly. Some implementations allow you to repeatedly emit a sequence of data, and some allow you to limit the number of repetitions.
Instead of creating a observable, it repeats the data sequence that emits the original observable, the number of launches is either infinite, or the number of repetitions is specified by repeat (n).
The repeat operator is executed by default on the trampoline scheduler. There is one variant that can specify scheduler with optional parameters.
- Repeat ()
- Repeat (Long))
- Repeat (Scheduler))
- Repeat (Long,scheduler)
The process examples are as follows:
Sample Program
//continuous generation of two sets (3,4,5) of numbersObservable.range (3,3). Repeat (2). Subscribe (NewSubscriber<integer> () {@Override Public void oncompleted() {System. out. println ("Sequence complete."); } @Override Public void OnError(Throwable e) {System. out. println ("Error:"+ e.getmessage ()); } @Override Public void OnNext(Integer i) {System. out. println ("Next:"+ i.tostring ()); } });
The results of the operation are as follows:
Next:3
Next:4
Next:5
Next:3
Next:4
Next:5
Sequence complete.
repeatWhen
It is not to cache and replay the original observable data series, but to conditionally re-subscribe and launch the original observable.
The termination notification (completion or error) of the original observable is passed to a notification processor as a void data, which determines whether to re-subscribe and launch the original observable. This notification processor is like a observable operator that accepts a observable that emits a void notification as input, returning a transmit void data (meaning, re-subscribing and emitting the original observable) or terminating directly (meaning, Use Repeatwhen to terminate the transmit data) of the observable.
The repeatwhen operator is executed by default on the trampoline scheduler. There is one variant that can specify scheduler with optional parameters.
- Repeatwhen (FUNC1)
- Repeatwhen (Func1,scheduler)
The process examples are as follows:
Sample Program
Observable.just (1,2,3). Repeatwhen (Newfunc1<observable<? Extends Void>, observable<?>> () {@Override PublicObservable<?>Pager(observable< extends void> Observable) {//Repeat 3 times returnObservable.zipwith (Observable.range (1,3),NewFunc2<void, Integer, integer> () {@Override PublicIntegerPager(Void aVoid, integer integer) {returnInteger }}). FlatMap (NewFunc1<integer, observable<?>> () {@Override PublicObservable<?>Pager(Integer integer) {System.out.println ("Delay repeat the"+ integer +"Count");//1 seconds repeat returnObservable.timer (1, timeunit.seconds); } }); }}). Subscribe (NewSubscriber<integer> () {@Override Public void oncompleted() {System.out.println ("Sequence complete."); }@Override Public void OnError(Throwable e) {System.err.println ("Error:"+ e.getmessage ()); }@Override Public void OnNext(Integer value) {System.out.println ("Next:"+ value); } });
The results of the operation are as follows:
Next:1
Next:2
Next:3
Repeat the 1 count
Next:1
Next:2
Next:3
Repeat the 2 count
Next:1
Next:2
Next:3
Repeat the 3 count
Next:1
Next:2
Next:3
Sequence complete.
empty/never/error
These operators are not executed by default on any particular scheduler, but empty and error have an optional parameter of scheduler, and if you pass the scheduler parameter, they will send a notification on the scheduler.
empty
Create a observable that does not emit any data but terminates normally.
Empty ()
never
Create a observable that does not emit data and does not terminate.
Never ()
error
Create a observable that does not emit data at the end of an error.
Error (Java.lang.Throwable)
Reference article links
- http://blog.csdn.net/job_hesc/article/details/46242117
- Https://github.com/ReactiveX/RxJava/wiki
Rxjava operator-Create type