rxJava, Refresh api data periodically

允我心安 提交于 2020-01-04 02:30:46

问题


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));
            }

This works as expected. my question is: how can i make this observer returns api response periodically?

let's say, user wants to refresh the data every 5 minutes


回答1:


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.


来源:https://stackoverflow.com/questions/34276498/rxjava-refresh-api-data-periodically

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