6

I want to insert a new user into my database only if a user with the same email does not exist.

Therefor I have two Observables: The first one emits a user with a specific email or completes without emitting anything. The second Observable inserts a new user and returns an Object of this newly created user.

My problem is, that I don't want the user emmitted by the first Observable (if existent) is transported to the Subscriber. Rather I would like to map it to null).

Observable<UserViewModel> observable = 
    checkAndReturnExistingUserObservable
        .map(existingUser -> null)
        .firstOrDefault(
            insertAndReturnNewUserObservable
                .map(insertedUser -> mMapper.map(insertedUser)
        )

This was the last thing I tried, but it says "cyclic inference" at the second map operator.

To summarize. I want to perform a second Observable only if the first completed empty but if not I don't want to return the Data emmitted by first rather I want to return null.

Any help is really appreciated.

1
  • I have an anwser about the operator to use to correctly emits your objects. However, I am not really sure about the cyclic inference. Can you post code of insertAndReturnNewUserObservable and the mMapper.map(...) just to be sure. Also, IMHO, returning null is a pretty bad idea. But I will give details in my answer after seeing your code ;)
    – mithrop
    Dec 10, 2015 at 8:56

1 Answer 1

13

There is the switchIfEmpty operator for this kind of operation:

checkAndReturnExistingUserObservable
.switchIfEmpty(insertAndReturnNewUserObservable)

Edit

If you don't need an existing user, there was a flatMap-based answer that turned the first check into a boolean value and dispatched based on its value:

checkAndReturnExistingUserObservable
.map(v -> true).firstOrDefault(false)
.flatMap(exists -> exists ? Observable.empty() : insertAndReturnNewUserObservable);
3
  • he also wants to emit a null value if empty, but I think switchIfEmpty is the best solution !
    – mithrop
    Dec 10, 2015 at 10:11
  • I'm not sure why the OP wants null if the user exists but you can map it in before the switchIfEmpty if necessary. The OP can also use a dummy user object instead of null or a new flag in the user object itself. Otherwise, there was a flatMap-based answer (now deleted).
    – akarnokd
    Dec 10, 2015 at 11:35
  • The problem with this approach is, that it will also return the found User of the first Observable (What I don't want). Currently I'm trying the flat map answer which seems to solve my problem in a clean way. Unfortunately it is deleted so I can't accept it as correct. Dec 10, 2015 at 11:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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.