rxJava,定期刷新api数据 [英] rxJava, Refresh api data periodically
本文介绍了rxJava,定期刷新api数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下 observable 来调用改造 api,然后将响应保存到缓存文件中:
I am using the following observable to call retrofit api then save the response into cache file:
@Override public Observable<StoryCollectionEntity> storyEntityList(final int page) {
return this.restApi.storyCollection(id, page)
.doOnNext(saveStoryCollectionToCacheAction)
.onErrorResumeNext(CloudNewsDataStore.this.mNewsCache.getStories(page));
}
这按预期工作.我的问题是:如何让这个观察者定期返回 api 响应?
This works as expected. my question is: how can i make this observer returns api response periodically?
假设用户希望每 5 分钟刷新一次数据
let's say, user wants to refresh the data every 5 minutes
推荐答案
interval()
操作符将在给定的时间间隔发出一个项目.
The interval()
operator will emit an item at a given time interval.
您可以使用它来触发周期性事件,如下所示:
You can use this to trigger periodic events like so:
Observable.interval(5, TimeUnit.MINUTES)
.flatMap(count -> this.restApi.storeCollection(id, page))
// etc.
这篇关于rxJava,定期刷新api数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文