Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rxjava behaviorsubject delete/clear value

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?

like image 285
msangel Avatar asked May 04 '17 03:05

msangel


1 Answers

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()
like image 112
Diolor Avatar answered Oct 20 '22 23:10

Diolor