【问题标题】:Rxjava : Maybe flatmap with completableRxjava:也许是带有可完成的平面图
【发布时间】:2018-10-13 04:43:51
【问题描述】:

我正在使用具有两个函数的外部 API,一个返回 Maybe,另一个返回 Completable(参见下面的代码)。我希望我的函数 'saveUser()' 返回一个Completable,这样我就可以使用doOnSuccess()doOnError 来检查它。但目前我的代码无法编译。另请注意,如果我的“getMaybe”没有返回任何内容,我想在我的平面图中获取一个空值作为参数,以便我可以处理空值与非空值的情况(如代码所示) .

    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)
                });
    }

【问题讨论】:

  • 你能把编译器错误放在问题里吗?

标签: java rx-java


【解决方案1】:

首先要记住,Maybe是用来获取一个元素的,空的或者错误的 我在下面重构您的代码以使可能返回 Completable

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】:

    这是我最终想出的解决方案。

        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);
                    });
        }
    

    【讨论】:

    • 实际上 defaultIfEmpty 正在检查 'map' 是否返回 'childUpdates' 它没有检查 getMaybe() 是否有空值
    猜你喜欢
    • 2015-06-20
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 2017-03-01
    相关资源
    最近更新 更多