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?
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();
}
});
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With