RxJava timer that repeats forever, and can be restarted and stopped at anytime
Solution 1
timer operator emits an item after a specified delay then completes. I think you looking for the interval operator.
Subscription subscription = Observable.interval(1000, 5000, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
public void call(Long aLong) {
// here is the task that should repeat
}
});
if you want to stop it you just call unsubscribe on the subscription:
subscription.unsubscribe()
Solution 2
Call Observable.repeat()
method to repeat
Disposable disposable = Observable.timer(3000, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.newThread())
.repeat()
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
If you want to stop it call disposable.dispose()
Solution 3
KOTLIN way
Observable.timer(5000, TimeUnit.MILLISECONDS)
.repeat() //to perform your task every 5 seconds
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
Log.d("ComingHere", "Inside_Timer")
}
Solution 4
This is the right and secure way %100 working :
//timer variable is in seconds unit
int timer = 5;
Disposable disposable = Observable.interval(timer, TimeUnit.SECONDS)
.map((tick) -> {
handler.post(() -> {
//Enter your CODE here !!!
});
return true;
}).subscribe();
And for stoping it :
if (disposable != null) {
disposable.dispose();
}
Related videos on Youtube
MBH
Software Engineer, Addicted to writing Mobile Applications for both platforms Android and iOS.
Updated on July 09, 2022Comments
-
MBH almost 2 years
In android i use Timer to execute task that repeats every 5 seconds and starts after 1 second in this way:
Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { // Here is the repeated task } }, /*Start after*/1000, /*Repeats every*/5000); // here i stop the timer timer.cancel();
this timer will repeat Until i call
timer.cancel()
I am learning RxJava with RxAndroid extension
So i found this code on internet, i tried it and it doesnt repeat:
Observable.timer(3000, TimeUnit.MILLISECONDS) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<Long>() { @Override public void call(Long aLong) { // here is the task that should repeat } });
so what is the alternative for the android Timer in RxJava.
-
Lennon Spirlandelli almost 7 yearsI just started learning RxJava and I'd like to know which one of those (Timer and RxJava) would be better in that approach? I mean related to performance and developing speed
-
Khurram Shehzad over 4 years
Observable.timer
will only fire once and then completes
-
-
Dave Moten almost 8 yearsYou don't need
.subscribeOn(Schedulers.io())
to start in background becauseObservable.interval
emits onSchedulers.computation()
by default. -
Akshay Mahajan about 6 yearsCan you tell how to reuse this timer..calling subscribe again will perform the same task twice in TimeUnit intervals.
-
Michał Klimczak almost 6 years@AkshayMahajan use publish() operator for that (possible with replay(1) depending on your case)
-
Ponomarenko Oleh over 5 yearsin RX 2 instead of Action1 you should use Consumer
-
omersem over 2 yearsIt's worth mentioning that the first param is the initial delay. public static Observable<Long> interval(long initialDelay, long period, TimeUnit unit) { return interval(initialDelay, period, unit, Schedulers.computation()); }