I have a function which should return a BehaviorSubject
. The subject is meant to return the most current version of a Profile
(user)Profile
is just a POJO that contains references to three members:
- a User
,
- that User's MeasurementList
,
- and a Deadline
.
Two of these properties is obtained via a retrofit call, and one of them is already held in a class variable.
Whenever the observable emits a new measurement list
or deadline
, the BehaviorSubject should emit a new updated Profile.
Here is a (hopefully helpful) diagram of what should be happening
This is what I have so far
public BehaviorSubject<Profile> observeProfile() {
if (profileBS == null) {
profileBS = BehaviorSubject.create();
Observable o = Observable.combineLatest(
Observable.just(userAccount),
observeMeasurements(),
observeDeadline(),
Profile::new
);
profileBS.subscribeTo(o); //subscribeTo does not exist, but this is what I am trying to figure out how to do.
}
return profileBS;
}
Can anyone help me to create this BehaviorSubject properly?
Thanks.
Subject
implements the Observer
interface so you can do the following
public BehaviorSubject<Profile> observeProfile() {
if (profileBS == null) {
profileBS = BehaviorSubject.create();
Observable o = Observable.combineLatest(
Observable.just(userAccount),
observeMeasurements(),
observeDeadline(),
Profile::new
);
// Note the following returns a subscription. If you want to prevent leaks you need some way of unsubscribing.
o.subscribe(profileBS);
}
return profileBS;
}
Note that you should come up with a way of handling the resulting subscription.