I'm using a RxAndroid observable
to retrieve some object (String in this case). My service looks like this:
public Observable<String> getRandomString() {
return Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
//code to retrieve result
subscriber.onNext("this is a string");
subscriber.onCompleted();
}
});
}
I subscribe in my presenter and post the result to the view:
public void loadRandomString() {
Observable<String> observable = mService.getRandomString();
observable
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.newThread())
.subscribe(new Subscriber<String>() {
@Override
public void onCompleted() { }
@Override
public void onError(Throwable e) {
mMainView.onError(e.getLocalizedMessage());
}
@Override
public void onNext(String string) {
//do something with string
}
});
}
This works fine and all, but I want this operation to be periodically (every x minutes). I could use a Timer
or ScheduledThreadPoolExecutor
to do this over and over again but i'd like to see if there is some solution within the realm of RxAndroid. I found some old solutions from 2013 but a lot of the code is deprecated at this time. Is this possible using some kind of recursion, or can I achieve this in a more elegant way?
Thanks in advance!
RxAndroid is an extension of RxJava for Android which is used only in Android application. RxAndroid introduced the Main Thread required for Android. To work with the multithreading in Android, we will need the Looper and Handler for Main Thread execution. RxAndroid provides AndroidSchedulers.
RxAndroid is a RxJava for Android extension that is only used in Android applications. RxAndroid added the Android-required Main Thread. We will need the Looper and Handler for Main Thread execution in order to work with multithreading in Android. Note: AndroidSchedulers are provided by RxAndroid.
Android Scheduler — This Scheduler is provided by rxAndroid library. This is used to bring back the execution to the main thread so that UI modification can be made. This is usually used in observeOn method.
RxJava is a Java library that enables Functional Reactive Programming in Android development. It raises the level of abstraction around threading in order to simplify the implementation of complex concurrent behavior.
What you probably want is Observable.interval()
. It emits on a timed interval. You can then flatmap that into your Observable<String>
, like so:
Observable.interval(3, TimeUnit.MINUTES)
.flatMap(new Func1<Long, Observable<String>>() {
@Override
public Observable<String> call(Long ignore) {
return getRandomString();
}
})
.subscribe(...insert your subscriber here...);
That said - if you're going to be doing this every few minutes, you might be better off looking into AlarmManager or JobScheduler, since chances are users won't be focused on your app for that long of a period of time.
As an aside, it'd be much easier to use Observable.just("this is a string")
than Observable.create()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With