0

有什么方法可以创建一个 RxJava2Observable来通知状态变化?像:

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

然后在任何地方使用它,例如:

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

每次状态更新都必须调用此订阅

4

1 回答 1

0

经过一番研究:

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)
    }

现在使用 normal subscribes 或 create new Observablefromsource

source.subscribe(...)
于 2018-07-13T19:03:01.237 回答