Rxjava special usage scenarios in the project

Source: Internet
Author: User

This article is based on:Rajava 2.1.14, in addition, this article does not introduce the use of operators, for details, please refer to Rxjava 2.x Operators

Reference:
    • rxjavas-repeatwhen-and-retrywhen-explained
    • Reflections on the Rxjava. Repeatwhen () and. Retrywhen () operators in the "translation"
Carousel changes This delay subscription time as needed

repeatThe event will be re-subscribed each time the callback is requested Observer.onComplete() or Subscriber.onComplete() before, and repeatWhen can be used to determine if it needs to cycle again, delay the subscription, change the delayed subscription time, and so on. This is more in line with the requirements of my project: A delay of one minute after the first request is completed, and if the request is not interrupted, it will change the subscription time to two minutes. Originally adopt repeatWhen + delay can achieve fixed carousel effect, but can not dynamically change the interval of the carousel, so instead of + repeatWhen flatMap + timer to achieve:

Observable.just(2)                .repeatWhen(objectObservable -> objectObservable                        .flatMap((Function<Object, ObservableSource<?>>) o -> Observable.timer(getRepeatInterval(), TimeUnit.SECONDS)))                .subscribeWith(new DebugResourceObserver<>());

getRepeatInterval()You can dynamically return the time you set

Interrupt Carousel

Using the above method can dynamically change the time of each re-subscription, and now we need to join the condition to interrupt the carousel. filterthe interrupt operation of the carousel can be implemented using the operator.

Observable.just(2)                .repeatWhen(objectObservable -> objectObservable                        .filterabort())                        .flatMap((Function<Object, ObservableSource<?>>) o -> Observable.timer(getRepeatInterval(), TimeUnit.SECONDS)))                .subscribeWith(new DebugResourceObserver<>());
Intervalrange

intervalRangeoperator, defined as follows:

longvalue1) has been reached.<p>@param start that start value of the range@paramif zero, the operator emits an onComplete after the initial delay@paramvalue (the start)@param period the period between subsequent values@param unit the unit of measure of the initialDelay and period amounts@returnnew Observable instance

Specific use:

Observable.just(2)                .Repeatwhen(NewFunction<observable<object>, observablesource<?>> () {@Override                     PublicObservablesource<?>Apply(observable<object> objectobservable)throwsException {returnObjectobservable.Filter(O-Abort())                                .FlatMap(Function<object, observablesource<?>>) O-Observable.Intervalrange(0,GetCount(),0,Getrepeatinterval(), Timeunit.SECONDS)); }                });

Temporarily involves some of their own "needs" on these! Hope to help everyone!

Rxjava special usage scenarios in the project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.