Daniil Orekhov
Daniil Orekhov

Reputation: 438

RxJava: How to extract object from observable?

I feel like this is a dumb question, but I couldn't find any answer for a while, so I'm gonna ask it, sorry :)

So, I need a function that does the following:

1) Calls another function to create an Observable User

2) Gets the User object from the Observable User

3) Gets some info about the user and runs through some logic

4) Returns Observable User

I am having troubles with step #2. How do I do that? Or, is this approach somehow fundamentally wrong?

Here's the "model" of the function:

@Override protected Observable buildUseCaseObservable(){

    Observable<User> userObservable = userRepository.findUserByUsername(username);

    //User user = ??????

    //if (...) {...}

    return userObservable;
}

Thank you :)

Upvotes: 13

Views: 28593

Answers (2)

paul
paul

Reputation: 13471

You can use operators(map, flatMap, doOnNext, etc) to get the object wrapped by your observable through the pipeline

     Observable.just("hello world")
               .map(sentence-> sentence.toUpperCase) --> do whatever you need.
               .subscribe(sentence -> println(sentence)

By design Observable follow the Observer patter, which subscribe to the observable and receive the item once has been emitted through the pipeline.

Also what you can do is instead use observer patter, just extract the object from the pipeline using toBlocking. But that´s is consider an anti pattern and means you´re not applying a good design.

          @Test
public void observableEvolveAndReturnToStringValue() {
    assertTrue(Observable.just(10)
                         .map(String::valueOf)
                         .toBlocking()
                         .single()
                         .equals("10"));
}

You can see more examples about to Blocking here https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/utils/ObservableToBlocking.java

Upvotes: 8

Rafay Ali
Rafay Ali

Reputation: 742

You cannot 'extract' something from an observable. You get items from observable when you subscribe to them (if they emit any). Since the object you are returning is of type Observable, you can apply operators to transform your data to your linking. The most common and easy to use operator in RxJava is 'map' which changes one form of data to other by applying a function.

In your case, you can use 'map' operator directly on Observable<user>:

return userRepository.findUserByUsername(username)    
       .map(new Func1<User, Object>() {
                @Override
                public Object call(User u) {
                    // ..
                    // apply your logic here
                    // ..
                    return myDataObject; // return you data here to subcribers
                }
            });

I hope you know the basics of RxJava and doesn't need any introduction about how to use operators. For map documentation, follow this link

Upvotes: 3

Related Questions