android - RxJava 和 Android : how to invalidate observable that use cache()?

标签 android caching rx-java retrofit2 rx-android

我有一个使用 RxJava 和 Retrofit2 加载数据的 Activity 。为了防止每次发生配置更改时都触发请求,我使用了 cache() 运算符。这很好用,我可以看到请求只发出一次。问题是,有时我需要发出新请求以获取新数据。换句话说,我需要使缓存无效。我该怎么做?

Activity 正在使用存储库发出请求:

public class Repository {
private final PlaceholderApi api;
private Observable<List<Post>> obs;
private static Repository inst;

public static Repository instance() {
    if (inst == null) {
        inst = new Repository();
    }
    return inst;
}

private Repository() {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> System.out.println(message));
    logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
    OkHttpClient okhttp = new OkHttpClient.Builder()
            .addInterceptor(logging)
            .build();
    Retrofit retrofit = new Retrofit.Builder()
            .client(okhttp)
            .addConverterFactory(MoshiConverterFactory.create())
            .baseUrl("http://jsonplaceholder.typicode.com/")
            .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
            .build();
    api = retrofit.create(PlaceholderApi.class);
}

public Observable<List<Post>> getAll() {
    if (obs == null) {
        obs = api.getPosts().cache();
    }
    return obs;
}
}

以及 Activity 代码:

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private Subscription sub1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sub1 = Repository.instance().getAll().subscribe(/* handle posts list */);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (sub1 != null && !sub1.isUnsubscribed()) {
        sub1.unsubscribe();
    }
}
}

最佳答案

这是一个使用来自 Dave Moten 的 answer 的 OnSubscribeRefreshingCache 包装类的示例用你的代码:

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private Subscription sub1;
private OnSubscribeRefreshingCache<List<Post>> postCache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        postCache = new OnSubscribeRefreshingCache(Repository.instance().getAll());
        sub1 = subscribe(Observable.create(postCache));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (sub1 != null && !sub1.isUnsubscribed()) {
            sub1.unsubscribe();
        }
    }

    private Subscription subscribe(Observable<List<Post> postObservable){
        return postObservable.subscribe(/* handle posts list */);
    }

    private void invalidateCache(){
        postCache.reset();
        sub1 = subscribe(Observable.create(postCache));
    }
}

然后在 Activity 生命周期方法之一或任何适合您的方法期间调用 invalidateCache() 方法。

关于android - RxJava 和 Android : how to invalidate observable that use cache()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39343819/

相关文章:

java - 如何将不同的并发策略分配给同一个(持久性)实体?

java - Apache Ignite - java.lang.ClassNotFoundException : Unknown pair

operators - RxJava 超时而不发出错误?

android - 为什么 RxJava 的 "subscribe"方法被多次调用?

android - 编写多个网络调用 RxJava - Android

android - 类似于24以下的GestureDescription执行点击,滑动, throw ?

java - ImageView,为什么大小不同?

android - 当我离开我的应用程序以更改设备字体并返回到我的应用程序时,它崩溃了。

java - 如何将文本设置为 ListView 中的多个选定项目

javascript - 使用 Javascript 加载和缓存图像并了解 HTTP 状态代码