RxCache - 防止 observable 被逐出

RxCache - Prevent observable from getting evicted

我在我的 Android 项目上使用 RxCache,当用户没有互联网连接时我遇到了问题。

如果服务器没有返回数据,是否有办法防止 observable 被逐出?

例如,每次用户刷新新闻提要(拉动刷新)时,都会调用 getRepository().getFeedPosts(tag, new EvictProvider(true));

如果用户突然失去与互联网的连接并再次刷新提要,则不会从服务器返回任何数据,observable 将被逐出并且它 returns 缓存版本(因为我正在设置 useExpiredDataIfLoaderNotAvailable(true) 在 RxCache 构建器上)。

同样在没有互联网连接的情况下,如果用户第二次刷新,则没有可用的缓存数据。

有没有办法防止这种情况发生?

谢谢

回答我自己的问题,我猜这是 RxCache 的限制。

这是 RxCache 的 Victor Albertos 告诉我的:

I don't think that's possible. Precisely, this is the way you evict the data of some provider, by creating an observable that just throws. I admit this is not optimal, but if you want to use a more mature API which handles these scenarios properly, I recommend to you to use ReactiveCache

我 运行 遇到了完全相同的问题,最后只是添加了一个互联网可用性检查来决定缓存是否应该失效。

public Boolean isOnline() {
    MyApplication application = MyApplication.sharedApplication();
    ConnectivityManager cm =
            (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

结合我缓存的 api 调用,如下所示:

public static  Single<List<People>> getPeopleAttending() {
    PartyManager manager = sharedManager();
    manager.cacheProvider.getPeopleAttending(manager.partyService.getAttending("Party/PeopleAttending"), new EvictProvider(isOnline()));
}

这可确保如果我的应用程序处于离线状态,它将获得缓存的结果。如果在线,它会一直从服务器获取当前参加聚会的人员名单。