簡體   English   中英

Rx Kotlin / Java可觀察到的狀態

[英]Rx Kotlin/Java Observable on state

有什么方法可以創建RxJava2 Observable來通知狀態更改? 喜歡:

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(...)

此訂閱必須在每個狀態更新時調用

經過研究:

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

現在使用常規的subscribe或從source創建新的Observable

source.subscribe(...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM