Reputation: 4719
I have a class with the following method which returns an Observable
:
public Observable<JsonObject> doSend(String message) {
CompletableFuture<JsonObject> resp = this.send(message);
return Observable.fromFuture(resp.whenComplete((response, err) -> {
response.success(callback -> {
return;
});
return ;
}));
The method is called from the following method of another class, where senderClass
is an instance of the class which contains the above method.
private void getData(Message<JsonObject> msgHandler) {
Observable<JsonObject> o = senderClass.doSend(...);
o.doOnComplete(() -> {
// do something
});
o.doOnEach(onNotification -> {
onNotification.getError();
});
}
When the method doSend
on the first class is called, the response
parameter in the handler correctly contains the result of the call to the send
message in the same class. The issue is that the value does not appear to be returned, and therefore cannot be accessed in the second class from the Observable which is returned from the doSend
method.
What is the correct way to write this so that the value can be set in the first class and accessed in the second?
Thanks
Upvotes: 0
Views: 665
Reputation: 3569
Observable
(along with the other reactive types) is designed with a fluent interface in which each invocation returns a new instance enhanced with the declared behavior.
to illustrate the point, this:
Observable<Object> o = newObservable();
o.doOnComplete(...);
o.doOnEach(...);
...is not the same as this:
Observable<Object> o = newObservable()
.doOnComplete(...)
.doOnEach(...);
in the former snippet, the calls to doOnComplete()
and doOnEach
create a new instance of Observable
, but since they are not assigned to a handler they are simply discarded without o
being enhanced with those behaviors. in other words - those methods do not mutate their receiver.
the latter example is the idiomatic approach. o
in this case is the accumulated result of applying the behaviors supplied to doOnComplete()
and doOnEach
to the originally constructed stream.
try updating getData()
using the second approach and see if that resolves your issue.
also, i think doSend()
can be simplified as:
Observable.fromFuture(this.send(message))
...unless of course you've got some other logic there that you've chosen to omit.
Upvotes: 1