Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make RxJava interval to perform action instantly

Hello I am making observable to ask my server about its online/offline status every 15 seconds:

public Observable<Response> repeatCheckServerStatus(int intervalSec, final String path) {
        return Observable.interval(intervalSec, TimeUnit.SECONDS)
                .flatMap(new Func1<Long, Observable<Response>>() {
                    @Override
                    public Observable<Response> call(Long aLong) {
                        return Observable.create(new Observable.OnSubscribe<Response>() {
                            @Override
                            public void call(Subscriber<? super Response> subscriber) {
                                try {
                                    Response response = client.newCall(new Request.Builder()
                                            .url(path + API_ACTION_CHECK_ONLINE_STATUS)
                                            .header("Content-Type", "application/x-www-form-urlencoded")
                                            .get()
                                            .build()).execute();

                                    subscriber.onNext(response);
                                    subscriber.onCompleted();
                                    if (!response.isSuccessful())
                                        subscriber.onError(new Exception());
                                } catch (Exception e) {
                                    subscriber.onError(e);
                                }
                            }
                        })
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread());
                    }
                });

    }

After I call this method, first execution of code will be after intervalSec time (15sec in my case). Looking at rxJava docummentation of interval method:

http://reactivex.io/documentation/operators/interval.html

This is how it should be.

Question: is there any way to execute code instantly and then repeat in intervals?

like image 267
F1sher Avatar asked Jan 23 '16 16:01

F1sher


People also ask

What is debounce in RxJava?

Using a debounce will remove some of the unnecessary query strings being sent over to the server. For example, if the debounce is set to have a 300 milliseconds interval, and if the typing speed is 1 letter per 100 millisecond, for the text abcdefg, it will be sending abc, abcdef and abcdefg to the server.

What is onNext in RxJava?

onNext(): This method is called when a new item is emitted from the Observable. onError(): This method is called when an error occurs and the emission of data is not successfully completed. onComplete(): This method is called when the Observable has successfully completed emitting all items.

What is Completable RxJava?

Single and Completable are new types introduced exclusively at RxJava that represent reduced types of Observable , that have more concise API. Single represent Observable that emit single value or error. Completable represent Observable that emits no value, but only terminal events, either onError or onCompleted.


2 Answers

You can execute it immediately also like this:

Observable.interval(0, 1000, TimeUnit.MILLISECONDS).subscribe();
like image 186
box Avatar answered Oct 01 '22 10:10

box


What you are looking for is startWith

Observable.interval(15, SECONDS).startWith(1);

This will get the updates from the interval, but emit one item immediately after subscribing.

like image 27
David Medenjak Avatar answered Oct 01 '22 11:10

David Medenjak