RxJava Creation Operator Notes

Keywords: Android Java

Course

Rx Java tutorial for parabolic God

rely on

compile 'io.reactivex:rxjava:1.0.14'
compile 'io.reactivex:rxandroid:1.0.1'

Create Operator

create

Create operation document

ABSTRACT: You can use the Create operator to create an Observable from scratch, pass it a function that accepts the observer as a parameter, and write the function to behave as an Observable -- appropriately invoking the observer's onNext, onError, and onCompleted methods.

The most common and basic method of creating Observable objects

 Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("hello");
                subscriber.onNext("rxjava");
                subscriber.onCompleted();
            }
        }).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.i(TAG, "call: "+s);
            }
        });

from

from document
Abstract: In RxJava, from operators can convert Future, Iterable, and arrays. For Iterables and arrays, the resulting Observable emits Iterables or each item of the array.

 String[] strs = {"hello","world","!"};
        //Convert an Iterable, a Future, or an array into an Observable
        //from is not executed on any particular scheduler by default. However, you can use Scheduler
        // As an optional second parameter passed to Observable, it manages the Future on that scheduler
        Observable.from(strs)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        Log.i(TAG, "call: " + s);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.i(TAG, "call: " + throwable.toString());
                    }
                }, new Action0() {
                    @Override
                    public void call() {
                        Log.i(TAG, "call: complete");
                    }
                });

just

just document
ABSTRACT: Just converts a single data into an Observable that transmits that data.
Just is similar to From, but From takes the array or Iterable data out and transmits it one by one, while Just simply transmits it as it is, and treats the array or Iterable as a single data.

Observable.just(1,2,3,4).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.i(TAG, "call: "+integer);
            }
        });

range

range document
Abstract: The Range operator emits an ordered sequence of integers in a range, and you can specify the start and length of the range.

RxJava implements this operator as a range function, which 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, Observable will not emit any data (if set to negative, an exception will be thrown).

range is not executed on any particular scheduler by default. One variant can specify Scheduler through optional parameters.

 Observable.range(3,4).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.i(TAG, "call: "+integer); // 3,4,5,6
            }
        });

Send 4 Integer s from 3

timer

timer document
Create an Observable that emits a special value after a given delay.

Observable.timer(1, TimeUnit.SECONDS).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                Log.i(TAG, "call: "+aLong); //Delay 1 second execution
            }
        });

Create a delayed task and fire it every second.

interval

interval document
Create an Observable that emits Integer Sequences at fixed intervals

Observable.interval(1, TimeUnit.SECONDS).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                Log.i(TAG, "call: "+aLong);//0,1,2,3,4,5......
            }
        });

Send Integer data every second.

repeat

repeat document
Create an Observable that emits specific data multiple times
RxJava implements this operator as a repeat method. Instead of creating an Observable, it repeats the original Observable data sequence, either infinitely or by specifying the number of repeats through repeat(n).

The repeat operator is executed on the trampoline scheduler by default. One variant can specify Scheduler through optional parameters.

Observable.just(2,3).repeat(2).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.i(TAG, "call: "+integer);
            }
        });

Re-launch Observable object twice

Posted by pmjm1 on Sat, 06 Apr 2019 16:24:29 -0700