亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Flowable 來自 Cache 和其他 Flowable for DataSource

Flowable 來自 Cache 和其他 Flowable for DataSource

紅顏莎娜 2022-12-15 16:36:47
我是 RxJava 的新手,我需要創建包含多個數據源的存儲庫。這對我來說很復雜,因為有幾個較小的子任務我不知道如何用 RxJava 實現。我有 Dao,它Flowable<Item>在一定范圍內提供給 DataSource 類。該數據源有本地緩存,可以隨時失效。當存儲庫向 DataSource 詢問某個范圍時(可能超出 DataSourse 范圍,邊界在完全緩存之前是未知的)它必須產生錯誤(或以其他方式通知 Repository)。我想Flowable<Item>為 DataSource 創建方法,它將從緩存中發出項目,如果需要,將它們與連接起來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;}在更高級別的存儲庫連接來自多個數據源的數據,因此必須有一種方法來連接來自多個來源的范圍,如果一個來源還不夠,則應該添加下一個來源的范圍。
查看完整描述

1 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

嘗試concatorconcatEager連接兩個可觀察值。也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);

}


查看完整回答
反對 回復 2022-12-15
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號