Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeatedly make API call with retrofit and rxjava

I have a retrofit observable:

@GET("something/")
Observable<Something> getSomething();

subscribing to it gives the response.

getSomething().subscribe(new Subscriber<Something>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(Something something) {
                //update database of something
            }
        });

how can i make this call every 60 seconds so that i can update database accordingly?

like image 616
Muhammad Arsal Avatar asked Mar 11 '23 05:03

Muhammad Arsal


1 Answers

First please don't do it if you can avoid it. It's preferable to push the changes (GCM for example) instead of pulling to save battery and data.

To do this you can use a combination of Observable.interval and the Observable.repeat operator.

Observable.interval(60, TimeUnit.SECONDS)
    .flatMap(n -> getSomething())
    .repeat()
    .subscribe();

Sorry about the lambdas.

like image 193
LordRaydenMK Avatar answered Mar 16 '23 23:03

LordRaydenMK