Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use scheduling with RxAndroid

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!

like image 771
Niels Masdorp Avatar asked Dec 15 '15 16:12

Niels Masdorp


People also ask

What is the difference between RxJava and RxAndroid?

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.

What is the use of RxAndroid?

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.

What is RX scheduler?

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.

What is RX Java used for?

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.


1 Answers

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().

like image 82
Dan Lew Avatar answered Nov 15 '22 07:11

Dan Lew