Reputation: 425
I am working on an android project that is using the reactive framework. I have an object with a data member that can change its value based on use input, say a "name". I need to listen for changes on this variable so I am creating a Behavior Subject for it. However, there are parts of the code where I need said value only once, say to pop up an alert on button click, I need to show it once and don't care about whether it changes or not. I don't know whether having this value as a behavior subject AND having a getter for it for when I need to access it once is a good thing, I'd rather have one or the other, and I need to listen for changes so I have to have the behavior subject. At the same time, it seems like an overkill to have to register for it, get the last value and de-register right after getting the value. Any suggestions?
Upvotes: 1
Views: 226
Reputation: 1168
You could always use take(1)
or similar to get the value you require and end the subscription immediately. This would keep the public API of the provider or wherever the Observable / Subject comes from cleaner as there wouldn't be the additional getter. This could make future changes to the provider easier as you don't need to update both the Observable and getter implementations.
Upvotes: 1