Repeatedly make API call with retrofit and rxjava

馋奶兔 提交于 2020-01-04 03:59:34

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/39309910/repeatedly-make-api-call-with-retrofit-and-rxjava

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!