java RxJava - 当返回可能为空时使用 flatmap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35345548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:02:10  来源:igfitidea点击:

RxJava - Using flatmap when return could be null

javarx-java

提问by Sree

Let's use this class for the example:

让我们以这个类为例:

 public static class CurrentUser{
    public static Observable<User> get(){
         //code basically returns the currently logged in User object
         //but sometimes there may not be a logged in user 
    }

    public static Observable<PutResult> logOut(){
        return get()

                //I only want to execute the following if user != null

                .flatMap(new Func1<User, Observable<PutResult>>() {
                    @Override
                    public Observable<PutResult> call(User user) {
                        //delete the session token and save
                        user.removeSessionToken();
                        return DatabaseModule.getStorIOSQLite()
                                .put()
                                .object(user)
                                .prepare()
                                .asRxObservable();
                    }
                });
    }

}

Should I just return null inside the flatmap? Are there any repercussions to this since it's expecting an Observable?

我应该在平面图中返回 null 吗?因为它期待一个 Observable,所以对此有任何影响吗?

回答by knutwalker

You should not return nullbut Observable.empty()instead.

你不应该返回,null而是Observable.empty()相反。

.flatMap(new Func1<User, Observable<PutResult>>() {
    @Override
    public Observable<PutResult> call(User user) {
        if (user == null) return Observable.empty();

        //delete the session token and save
        user.removeSessionToken();
        return DatabaseModule.getStorIOSQLite()
                .put()
                .object(user)
                .prepare()
                .asRxObservable();
    }
});

回答by Malt

It isn't clear what nullyou're talking about.

不清楚null你在说什么。

If you're asking whether the Observable<PutResult>can emit a PutResultthat's null, then yes. This would result in a nullbeing emitted by the outer observable.

如果你问是否Observable<PutResult>可以发出PutResultthat's null,那么是的。这将导致null外部 observable 发出a 。

If you're asking whether the Observable<PutResult>returned by the Func1can be null, then no. Return Observable.empty()instead, (or Observable.just(null)or similar if you need to keep track of the number of emitted items).

如果您要问Observable<PutResult>返回的是否Func1可以为空,则否。返回Observable.empty(),(或Observable.just(null)类似,如果您需要跟踪发出的项目数量)。

回答by rana

In RxJava 2 null is considered to be an invalid value. You can use Maybe component in those cases , some thing like the below

在 RxJava 2 中,null 被认为是无效值。您可以在这些情况下使用 Maybe 组件,如下所示

  Maybe<Result>

   or 

  You can use flatMapMaybe<Result>