有什么方法可以创建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 statestate.subscribe(...)此订阅必须在每个状态更新时调用
1 回答
![?](http://img1.sycdn.imooc.com/545863e80001889e02200220-100-100.jpg)
PIPIONE
TA贡献1829条经验 获得超9个赞
经过一些研究:
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)
}
现在使用normalsubscribe或Observable从中创建newsource
source.subscribe(...)
添加回答
举报
0/150
提交
取消