Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create BehaviorSubject from Observable

Say I have an observableA which gets data from network and emits it, if I do subscription to this observable each time it tries to request data from network which is heavy.

I want to create BehaviorSubject and connect it to observableA, so that any other threads/objects will subscribe to BehaviorSubject in order to get latest emitted data.

So far I couldnt manage it to code. I cannot create empty BehaviorSubject and call inside observableA, because they are not related to each other. I cannot subscribe to observableA and get BehaviorSubject as observer, any idea how can I accomplish it? or maybe even better?

like image 345
user1831986 Avatar asked Jun 05 '14 20:06

user1831986


1 Answers

You can use multicast. E.g.,

Observable<String> o = ...;
ConnectableObservable<String> co = o.multicast(BehaviorSubject.<String> create());
co.connect();
co.subscribe(...);
co.subscribe(...);
co.subscribe(...);
like image 134
zsxwing Avatar answered Nov 16 '22 12:11

zsxwing