问题
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