1
我想缓存retrofit2 api响应observable的最佳方法是与behaviorSubject。这会发出最后发送的项目。所以我试图做一个函数,将采取一个布尔缓存参数,以了解是否应该从缓存或retrofit2调用中检索响应。 retrofit2调用只是返回一个observable。但是让我们看看我想要什么:Rxjava2 - 如何缓存Observable
下面是函数之前,我实现了高速缓存,它只是简单地做了一个retrofit2调用get订阅了它在其他地方一个API响应和某人:
public Observable<List<CountryModel>> fetchCountries() {
CountriesApi countriesService = mRetrofit.create(CountriesApi.class);
return countriesService.getCountries();
}`
,这里是什么我想实现,但很难实现一个行为主体来做到这一点?或者我还可以如何缓存响应?
public Observable<List<CountryModel>> fetchCountries(boolean cache) {
CountriesApi countriesService = mRetrofit.create(CountriesApi.class);
if(!cache){
//somehow here i need to wrap the call in a behaviorsubject incase next time they want a cache - so need to save to cache here for next time around but how ?
return countriesService.getCountries();
} else{
behaviorsubject<List<CountryModel>>.create(countriesService.getCountries())
//this isnt right. can you help ?
}
}`