7

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?

3 Answers 3

22

You should not return null but Observable.empty() instead.

.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();
    }
});
2
  • So, I just realized something. If I call Oberservable.empty(), onComplete gets called immediately and it ends the chain. So if the user is null, I can't do logOut().anotherOperator()
    – Sree
    Feb 29, 2016 at 23:52
  • 2
    You could use Observable.just(null) (or some form of non-null PutResult that carries the information of a non-existing user) in that case. It doesn't complete right away, but you have to do null checks on every subsequent operator. There is also never, which will never complete on its own, so you have to use sth like limit or flatMap an empty into the stream eventually.
    – knutwalker
    Mar 1, 2016 at 0:21
6

It isn't clear what null you're talking about.

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

If you're asking whether the Observable<PutResult> returned by the Func1 can 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).

0
4

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

  Maybe<Result>

   or 

  You can use flatMapMaybe<Result>
1
  • 1
    That's true. Also, you can use observable.flatMapMaybe or single.flatMapMaybe instead of flatMap. Apr 16, 2019 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.