0

Is any way to create an RxJava2 Observable to notify state changes? like:

private var internalState = "state1"
val state: Observable<String> = Observable(...)
...
fun updateState(newState: String) { ... } // !!! attention: I need to notify all subscribers about this new state

then use it every where like:

// observe on state
state.subscribe(...)

this subscription must be called every state updates

Hossain Khademian
  • 1,616
  • 3
  • 19
  • 29
  • note: I can use `Observable.defer` or `Observable.fromCallback` to create `Observable` but I need a way to re-notify all subscribers about data change, without new subscribe need – Hossain Khademian Jul 13 '18 at 18:33

1 Answers1

0

After some research:

val source = PublishSubject.create<String>()

var state = "state1"
    get() = field // redundant: only to show it's possible to get state directly: maybe for class internal usages
    set(value) {
        field = value
        source.onNext(field)
    }

Now use normal subscribes or create new Observable from source

source.subscribe(...)
Hossain Khademian
  • 1,616
  • 3
  • 19
  • 29