Rxjava2 如何 return 如果时间不出来缓存值
Rxjava2 how to return cached values if time does't come out
我有一个问题。我有简单的 Observable。
public interface CitiesApi {
@GET("location/cities")
Observable<List<City>> getCities(@Query("country") String countryId);
}
我还有 class(manager),它保存来自这个 observable 的数据并将其交给 activity 或演示者。
public class Manager {
@Inject
CitiesApi mCitiesApi;
private Date mDate;
private List<City> mCities;
public Observable<List<City>> getObservable() {
return mCitiesApi.getCities("123");
}
}
问题:当我订阅这个 observable 时,
current time - last subscribe time < 10 min(or other range, it doesn't
matter...)
我想用旧数据调用onNext。但如果时差 > 10,我想从网络下载数据(return 原始可观察)。我不想使用改装缓存,因为我可以手动更改此列表。
您的 Observable 必须看起来像这样:
public Observable<List<City>> getObservable() {
if (cacheTime > 10){ //If cached data time is greater than 10min, you make your network call.
return mCitiesApi.getCities("123");
} else{
return Observable.just(getChachedData("123")) //Where getChachedData is a method that return a list of your cached data.
}
}
我有一个问题。我有简单的 Observable。
public interface CitiesApi {
@GET("location/cities")
Observable<List<City>> getCities(@Query("country") String countryId);
}
我还有 class(manager),它保存来自这个 observable 的数据并将其交给 activity 或演示者。
public class Manager {
@Inject
CitiesApi mCitiesApi;
private Date mDate;
private List<City> mCities;
public Observable<List<City>> getObservable() {
return mCitiesApi.getCities("123");
}
}
问题:当我订阅这个 observable 时,
current time - last subscribe time < 10 min(or other range, it doesn't matter...)
我想用旧数据调用onNext。但如果时差 > 10,我想从网络下载数据(return 原始可观察)。我不想使用改装缓存,因为我可以手动更改此列表。
您的 Observable 必须看起来像这样:
public Observable<List<City>> getObservable() {
if (cacheTime > 10){ //If cached data time is greater than 10min, you make your network call.
return mCitiesApi.getCities("123");
} else{
return Observable.just(getChachedData("123")) //Where getChachedData is a method that return a list of your cached data.
}
}