The author blog

    http://blog.csdn.net/u014165119

preface

Understand the basics of RxJava before reading this article.

directory

  1. Creation of observables

    1. from()

    2. just()

    3. create()

    4. interval()

    5. timer()

    6. range()

    7. error()    

    8. defer()

  2. redo

    1. repeat()

    2. repeatWhen()

  3. retry

    1. retry()

    2. retryWhen()

  4. transform

    1. map()

    2. flatMap()

  5. filter

    1. filter()

    2. first()

    3. last()

    4. skip()

    5. skipLast()

    6. take()

    7. takeLast()

    8. sample()

    9. elementAt()

    10. elementAtOrDefault()

    11. ignoreElements()

  6. Function implementation

    1. Delay traversal

    2. The countdown


Creation of observables



from()



public static <T> Observable<T> from(Iterable<? extends T> iterable);


Transform the collection into an Observable that emits one element of the collection at a time.

Usage scenario: Iterate over collections (arrays, lists, etc.).



Other form() apis:



  • public static <T> Observable<T> from(Future<? extends T> future);


  • public static <T> Observable<T> from(Future<? extends T> future, long timeout, TimeUnit unit);


  • public static <T> Observable<T> from(Future<? extends T> future, Scheduler scheduler);


  • public static <T> Observable<T> from(T[] array);



For example:





just()


public static <T> Observable<T> just(final T value);


Transforms one or more objects into observables that emit each Object in turn.

Usage scenarios: Convert one or more common objects into Observables, such as database query results or network query results.



Other Just () apis:


  • public static <T> Observable<T> just(T t1, T t2);


  • public static <T> Observable<T> just(T t1, T t2, T t3);


  • public static <T> Observable<T> just(T t1, T t2, T t3, T t4);


  • public static <T> Observable<T> just(T t1, T t2, T t3, T t4, T t5);


  • public static <T> Observable<T> just(T t1, T t2, T t3, T t4, T t5, T t6);


  • public static <T> Observable<T> just(T t1, T t2, T t3, T t4, T t5, T t6, T t7);


  • public static <T> Observable<T> just(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8);


  • public static <T> Observable<T> just(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8, T t9);


  • public static <T> Observable<T> just(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8, T t9, T t10);


For example:



The just() method takes 1 to 10 arguments, which means you can use just() or from() when the number of elements is less than or equal to 10, and from() otherwise.


create()


public static <T> Observable<T> create(OnSubscribe<T> f);


Observable returns an Observable that executes a specific method when OnSubscribe. Scenarios: Deprecated. Use other operators instead, such as the from() operator for traversal.


Other CREATE () apis:


  • @Beta public static <S, T> Observable<T>create(SyncOnSubscribe<S, T> syncOnSubscribe);


  • @Experimental public static <S, T> Observable<T> create(AsyncOnSubscribe<S, T> asyncOnSubscribe);


For example:



This method is not commonly used. Most of the time, we use methods like just() and form().


interval()


public static Observable<Long> interval(long interval, TimeUnit unit);


Returns an Observable that emits a serial number at specified intervals. Usage scenario: You can use this operator to perform functions such as timing and countdown.


Other interval() apis:


  • public static Observable<Long> interval(long interval, TimeUnit unit, Scheduler scheduler);


  • public static Observable<Long> interval(long initialDelay, long period, TimeUnit unit);


  • public static Observable<Long> interval(long initialDelay, long period, TimeUnit unit, Scheduler scheduler);


For example:




timer()


public static Observable<Long> timer(long delay, TimeUnit unit);


Observable creates an Observable that emits a data (fixed value: 0) after the specified delay. Usage scenario: It can be used to complete the timing function.



Other Timer () apis:


  • public static Observable<Long> timer(long delay, TimeUnit unit, Scheduler scheduler);


For example:




 range()


public static Observable<Integer> range(int start, int count);


Creates an Observable that emits contiguous integers in the specified range. Usage scenario: You can use this operator to complete a fori loop, such as for(int I =5; i<=7; I++) -> observable. range(5, 3).


Other Range () apis:


  • public static Observable<Integer> range(int start, int count, Scheduler scheduler);


For example:




error()


public static <T> Observable<T> error(Throwable exception);


Create an Observable that emits onError notifications without emitting any data. Usage scenario: After an exception is caught in a program, you can use this operator to pass the caught exception to subsequent logic for processing.


For example:



defer()


public static <T> Observable<T> defer(Func0<Observable<T>> observableFactory);


Observables are created when subscribing; Each subscription creates a new Observable. Usage scenario: You can use this operator to encapsulate functions that need to be executed more than once.


For example:



2

redo


repeat()


public final Observable<T> repeat();


Make the Observable emit data repeatedly after an onNext() notification. An onComplete() notification is issued when the redo is complete, and an onError() notification is issued if an exception occurs during the redo. Usage scenario: You can use this operator to repeat the execution of the previous task immediately after the completion of a task, for example, sending multiple network requests.


Other Repeat () apis:


  • public final Observable<T> repeat(final long count);


  • public final Observable<T> repeat(Scheduler scheduler);


  • public final Observable<T> repeat(final long count, Scheduler scheduler);


For example:




repeatWhen()


public final Observable<T> repeatWhen(final Func1<? super Observable<? extends Void>, ? extends Observable<? >> notificationHandler)


Makes an Observable conditionally emit data after an onNext() notification. An onCompleted() notification is emitted when the redo is complete, and an onError() notification is emitted if an exception occurs during the redo. Usage scenario: You can use this operator to specify that a task is repeated when certain conditions are met, such as sending multiple network requests.


Other repeatWhen() APIS:


  • public final Observable<T> repeatWhen(final Func1<? super Observable<? extends Void>, ? extends Observable>? > notificationHandler, Scheduler scheduler);


For example:




3

retry


retry()


public final Observable<T> retry();


An onError() notification is not issued directly when an error occurs in the executing Observable sequence. Instead, a new subscription to the Observable is issued until no exception occurs during the redo process. OnNext () and onCompleted() notifications are issued. If an exception occurs during the redo, it will continue to retry until the maximum number of retries is reached, after which the latest onError() notification will be issued.

Application scenario: If an exception occurs in a network request, resend the request.



Other Retry () apis:


  • public final Observable<T> retry(final long count);


  • public final Observable<T> retry(Func2<Integer, Throwable, Boolean> predicate);


For example:




retryWhen()


public final Observable<T> retryWhen(final Func1<? super Observable<? extends Throwable>, ? extends Observable<? >> notificationHandler);


Conditional execution retry. Application scenario: If a network request fails, the system resends the request if certain conditions are met.


Other retryWhen() APIS:


  • public final Observable<T> retryWhen(final Func1<? super Observable<? extends Throwable>, ? extends Observable<? >>notificationHandler, Scheduler scheduler);


For example:




4

transform



map()


public final <R> Observable<R> map(Func1<? super T, ? extends R> func);


Applies elements emitted by the source Observable to the specified function and sends the result of the function.

Usage scenario: Converts data obtained from the network (NetData objects) into database related objects (DBData objects) and sends it as an Observable.



For example:




flatMap()


public final <R> Observable<R> flatMap(Func1<? super T, ? extends Observable<? extends R>> func);


Converts the source Observable to another Observable.

Usage scenarios: Get data from the network and emit it using obsA objects. The flatMap() operator stores the data into the database and returns a new object obsB.


Other flatMap() apis:


  • @Beta public final <R> Observable<R> flatMap(Func1<? super T, ? extends Observable<? extends R>> func, int maxConcurrent);


  • public final <R> Observable<R> flatMap(Func1<? super T, ? extends Observable<? extends R>> onNext, Func1<? super Throwable, ? extends Observable<? extends R>> onError, Func0<? extends Observable<? extends R>> onCompleted);


  • @Beta public final <R> Observable<R> flatMap(Func1<? super T, ? extends Observable<? extends R>> onNext, Func1<? super Throwable, ? extends Observable<? extends R>> onError, Func0<? extends Observable<? extends R>> onCompleted, int maxConcurrent);


  • public final <U, R> Observable<R> flatMap(final Func1<? super T, ? extends Observable<? extends U>> collectionSelector, final Func2<? super T, ? super U, ? extends R> resultSelector);


  • @Beta public final <U, R> Observable<R> flatMap(final Func1<? super T, ? extends Observable<? extends U>> collectionSelector, final Func2<? super T, ? super U, ? extends R> resultSelector, int maxConcurrent);



For example:



5

filter



filter()


public final Observable<T> filter(Func1<? super T, Boolean> predicate);


Only elements that satisfy the specified predicate are emitted. Usage scenario: Filter can be used instead of the IF statement.



For example:





first()


public final Observable<T> first();


Returns an Observable that emits only the first element emitted by the source Observable. If the source Observable is empty, a NoSuchElementException is thrown. Usage scenario: Sends multiple packets in sequence and receives only the first packet.



Other FIRST () apis:


  • public final Observable<T> first(Func1<? super T, Boolean> predicate)


For example:




last()


public final Observable<T> last();


Returns an Observable that emits only the last-to-last element that the source Observable emits. If the source Observable is empty, a NoSuchElementException is thrown. Usage scenario: Sends multiple packets in sequence and receives only the last packet.



Other LAST () apis:


  • public final Observable<T> last(Func1<? super T, Boolean> predicate)



For example:




skip()


public final Observable<T> skip(int count);


Skip the previously specified number of elements or the specified period of time, only the following elements.



Other Skip () apis:


  • public final Observable<T> skip(long time, TimeUnit unit);


  • public final Observable<T> skip(long time, TimeUnit unit, Scheduler scheduler);


For example:



skipLast()


public final Observable<T> skipLast(int count);


Skip the previously specified number of elements or the specified period of time, only the following elements. Any data emitted by the source Observable is deferred when specified.


Other skipLast() apis:


  • public final Observable<T> skipLast(long time, TimeUnit unit);


  • public final Observable<T> skipLast(long time, TimeUnit unit, Scheduler scheduler);


For example:



take()


public final Observable<T> take(final int count);


Emits only the previously specified number of elements or for the specified time.


Other take() apis:


  • public final Observable<T> take(long time, TimeUnit unit);


  • public final Observable<T> take(long time, TimeUnit unit, Scheduler scheduler);


For example:



takeLast()


public final Observable<T> takeLast(final int count);


Only the number of elements specified later or for a specified time is emitted. Any data emitted by the source Observable is deferred when specified.


Other takeLast() apis:


  • public final Observable<T> takeLast(int count, long time, TimeUnit unit);


  • public final Observable<T> takeLast(int count, long time, TimeUnit unit, Scheduler scheduler);


  • public final Observable<T> takeLast(long time, TimeUnit unit);


  • public final Observable<T> takeLast(long time, TimeUnit unit, Scheduler scheduler);


For example:



sample()


public final Observable<T> sample(long period, TimeUnit unit);


Periodically emits the last piece of data emitted by an Observable.


Other sample() APIS:


  • public final Observable<T> sample(long period, TimeUnit unit, Scheduler scheduler);


  • public final <U> Observable<T> sample(Observable<U> sampler);


For example:



elementAt()


public final Observable<T> elementAt(int index);


Emits only elements with the specified index. Usage scenario: index to elements in a collection, etc.



For example:




elementAtOrDefault()


public final Observable<T> elementAtOrDefault(int index, T defaultValue);


Emits only the element of the specified index, or the default value if the element of the index does not exist.



For example:



// Launch data with index 9, if not present, launch -5

ignoreElements()


public final Observable<T> ignoreElements();


An onCompleted() notification is issued without sending any data.



For example:



6

Function implementation


Delay traversal



The countdown