繁体   English   中英

可以使用RxJava从Cache和其他Flowable for DataSource流出

[英]Flowable from Cache and other Flowable for DataSource using RxJava

我是RxJava的新手,我需要创建包含多个数据源的存储库。 这对我来说很复杂,因为有几个较小的子任务,我不知道如何用RxJava实现。

我有Dao,它在某些范围内为DataSource类提供了Flowable<Item> 此数据源具有本地缓存​​,可以随时使其无效。 当存储库向DataSource请求某个范围(可能超出DataSourse边界,边界在完全缓存之前是未知的)时,它必须产生错误(或以其他方式通知存储库)。

我想为DataSource创建Flowable<Item>方法,它将从缓存中发出项目,如果需要,可以使用Flowable<Item> dao.getRange(...)连接它们,同时缓存来自dao的新项目。 此外,我需要处理来自dao的错误,他们必须处理或转换为更高级别的错误。

DataSource.class

List<Item> cache;

Flowable<Item> getRange(int start, int amount) {

    final int cacheSize = cache.size();
    final int canLoadFromCache = cacheSize - start;
    final int loadFromDao = amount - canLoadFromCache;

    if (isCorrupted) return Flowable.fromCallable(() -> {
        throw new Exception("CorruptedDatasource");
    });

    Flowable<Item> cacheFlow = null;
    Flowable<Item> daoFlow = null;

    if (canLoadFromCache > 0) {
        cacheFlow = Flowable.fromIterable(
                cache.subList(start, canLoadFromCache)
        );

        daoFlow = dao.getRange(
                uri, 
                cacheSize, //start
                loadFromDao //amount
        );
    } else {
        if (isFullyCached) return Flowable.fromCallable(() -> {
            throw new Exception("OutOfBounds");
        });

        //To not deal with gaps load and cache data between;
        //Or replace it with data structure which can handle for us;
        daoFlow = dao.getRange(
                uri,
                cacheSize,
                start - cacheSize + amount);
        //all these items should be cached;
        //other cached and put downstream;
        //Dao errs should be converted to higher lever exceptions,
        //Or set flags in DataSource;
    }
    // return concatenated flowable;
}

在更高级别的存储库连接来自多个DataSource的数据,因此必须有一种方法可以从某个方式来源于来自多个源的范围,如果还没有,则应该添加下一个范围。

请帮我!

尝试连接两个observable的concatconcatEager doOnNext()doOnError()也可以帮助您缓存和错误处理

List<Item> cache;

Flowable<Item> getRange(int start, int amount) {

    ...
        if (isFullyCached) return Flowable.fromCallable(() -> {
            throw new Exception("OutOfBounds");
        });

        //To not deal with gaps load and cache data between;
        //Or replace it with data structure which can handle for us;
        daoFlow = dao.getRange(
                uri,
                cacheSize,
                start - cacheSize + amount);
        //all these items should be cached;
        //other cached and put downstream;
            .doOnNext(result -> /* insert caching logic here */)
        //Dao errs should be converted to higher lever exceptions,
        //Or set flags in DataSource;
            .doOnError(error -> /* handle error here */)
            .onErrorReturn(/* and/or return some empty item */)
    }
    // return concatenated flowable;
    return cacheFlow.concat(daoFlow);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM