第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何從訂閱者返回 Observable?

如何從訂閱者返回 Observable?

忽然笑 2021-10-27 16:57:25
我第一次在我的應用程序中使用 rxJava 我試圖實現(xiàn)以下實現(xiàn):從第 3 方服務器獲取帳戶從本地數(shù)據(jù)庫獲取帳戶比較帳戶并過濾掉那些不在本地數(shù)據(jù)庫中的帳戶只保存那些不在本地數(shù)據(jù)庫中的本地數(shù)據(jù)庫中的帳戶。這是我的代碼:- private Observable<List<Result<Account, IError>>> filterAccounts(Context context, List<Account> accounts){         accountDAL.getByIds(context, accounts                .stream()                .map(a -> Long.valueOf(a.getAccountId()))                .collect(Collectors.toList()))//return Observable<List<T>> getByIds(Context context, List<Long> ids)                .map( a -> {                    Map<Long, SearchConnectAccount> map = a.stream()                            .collect(Collectors.toMap(a -> a.getId(), Function.identity())); // map ==> {id = Account}                 return map;                }).subscribe( seMap -> { // subscribe observable                  List<Account> filteredList = accounts.stream()                             .filter(a -> seMap.get(Long.valueOf(a.getAccountId())) == null)                             .collect(Collectors.toList());Observable<List<Result<Account, IError>>> o = accountDAL.save(context, filteredList).first();                    return o;//accountDAL.save(context, filteredList).first();         });        // how to return Observable<List<Result<Account, IError>>> from here    }任何幫助表示贊賞。
查看完整描述

1 回答

?
慕標5832272

TA貢獻1966條經(jīng)驗 獲得超4個贊

你可以這樣做,


private Observable<List<Result<Account, IError>>> filterAccounts(Context context, List<Account> accounts){

     return accountDAL.getByIds(context, accounts

            .stream()

            .map(a -> Long.valueOf(a.getAccountId()))

            .collect(Collectors.toList()))

            .map(a -> 

                 a.stream()

                        .collect(Collectors.toMap(a -> a.getId(), Function.identity())) // map ==> {id = Account}


            ).map(seMap -> 

               accountDAL.save(context, accounts.stream()

                     .filter(a -> seMap.get(Long.valueOf(a.getAccountId())) == null)

                     .collect(Collectors.toList())).first());

}

更新


第二次調(diào)用save返回一個Observable<?>(只是一個假設),當它被包裝在一個map運算符中時,它返回Observable<Observable<?>>。但是你需要的返回值是Observable<?>. 所以,你需要拼合Observable<Observable<?>>到Observable<?>哪里,這就是flatMap被使用。如果需要,這里是更新的答案。


private Observable<List<Result<Account, IError>>> filterAccounts(Context context, List<Account> accounts) {

        return accountDAL

                .getByIds(context,

                        accounts.stream().map(a -> Long.valueOf(a.getAccountId())).collect(Collectors.toList()))

                .map(ar -> ar.stream().collect(Collectors.toMap(Account::getAccountId, Function.identity())) // map ==>

                                                                                                            // {id =

                // Account}


                ).flatMap(seMap -> accountDAL.save(context, accounts.stream()

                        .filter(a -> seMap.get(Long.valueOf(a.getAccountId())) == null).collect(Collectors.toList())));

    }

查看完整回答
反對 回復 2021-10-27
  • 1 回答
  • 0 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號