代码之家  ›  专栏  ›  技术社区  ›  PerrierCitror

Rxjava:可能是带有completable的flatmap

  •  2
  • PerrierCitror  · 技术社区  · 7 年前

    我使用一个带有两个函数的外部API,一个返回 Maybe Completable (见下面的代码)。我希望函数“saveUser()”返回 doOnSuccess() doOnError . 但目前我的代码无法编译。另外请注意,如果我的'getMaybe'没有返回任何东西,我希望在flatmap中得到一个null值作为参数,这样我就可以处理null和notnull的情况(如代码中所示)。

        private Maybe<DataSnapshot> getMaybe(String key) {
            // external API that returns a maybe
        }
    
        private Completable updateChildren(mDatabase, childUpdates) {
            // external API that returns a Completable
        }
    
        // I'd like my function to return a Completable but it doesn't compile now
        public Completable saveUser(String userKey, User user) {
            return get(userKey)
                    .flatMap(a -> {
                        Map<String, Object> childUpdates = new HashMap<>();
    
                        if (a != null) {
                            // add some key/values to childUpdates
                        }
    
                        childUpdates.put(DB_USERS + "/" + userKey, user.toMap());
    
                        // this returns a Completable
                        return updateChildren(mDatabase, childUpdates)
                    });
        }
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   dmarquina    7 年前

    首先,记住Maybe是用来获取一个元素的,无论是空的还是错误的 我重构了下面的代码,使posible返回一个可完成的

    public Completable saveUser(String userKey, User user) {
        return getMaybe(userKey)
                .defaultEmpty(new DataSnapshot)
                .flatMapCompletable(data -> {
                    Map<String, Object> childUpdates = new HashMap<>();
    
                    //Thanks to defaultempty the object has an 
                    //Id is null (it can be any attribute that works for you) 
                    //so we can use it to validate if the maybe method
                    //returned empty or not
                    if (data.getId() == null) {
                        // set values to the data
                        // perhaps like this
                        data.setId(userKey);
                        // and do whatever you what with childUpdates
                    }
    
                    childUpdates.put(DB_USERS + "/" + userKey, user.toMap());
    
                    // this returns a Completable
                    return updateChildren(mDatabase, childUpdates);
                });
    }
    
        2
  •  1
  •   PerrierCitror    7 年前

    这就是我最终想出的解决办法。

        public Completable saveUser(String userKey, User user) {
            return getMaybe(userKey)
                    .map(tripListSnapshot -> {
                        Map<String, Object> childUpdates = new HashMap<>();
                        // // add some key/values to childUpdates
                        return childUpdates;
                    })
                    .defaultIfEmpty(new HashMap<>())
                    .flatMapCompletable(childUpdates -> {
                        childUpdates.put(DB_USERS + "/" + userKey, user.toMap());
                        return updateChildren(mDatabase, childUpdates);
                    });
        }