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
repeat
The 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. filter
the 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
intervalRange
operator, 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