Rx Kotlin / Java可观察到的状态

有什么方法可以创建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(...)

此订阅必须在每个状态更新时调用


心有法竹
浏览 137回答 1
1回答

PIPIONE

经过一些研究:val source = PublishSubject.create<String>()var state = "state1"&nbsp; &nbsp; get() = field // redundant: only to show it's possible to get state directly: maybe for class internal usages&nbsp; &nbsp; set(value) {&nbsp; &nbsp; &nbsp; &nbsp; field = value&nbsp; &nbsp; &nbsp; &nbsp; source.onNext(field)&nbsp; &nbsp; }现在使用normalsubscribe或Observable从中创建newsourcesource.subscribe(...)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java