I have an application that uses BehaviorSubject
as memory storage for some value. This value is setting on app start based on the result of REST API request if the user is logined OR during user login.
But when the user is do logging out, the BehaviorSubject keep old value. Is there any way to clear the BehaviorSubject
and force it have hasValue()
as false on demand?
The short answer is no.
Once a Subject receives at least one value hasValue
will always return true. A usual trick in those cases is to have a wrapping class. Here's an example with Optional
:
Subject subject = BehaviorSubject.<Optional<String>>create()
// add
subject.accept(Optional.of("Hello"))
// "clear" value
subject.accept(Optional.empty())
// check
subject.value.isPresent()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With