我有一個基本模擬狀態圖的可觀察流。例如:通過可觀察流循環
whenThisHappens()
.switchMap(i -> {
if (i.isThisThing()) {
return whenThatHappens();
} else {
return nothingHappened();
}
}
.subscribe(thing -> {
if (thing.isThatThing()) {
log("Got that thing");
} else {
log("Got nothing");
}
});
的問題是,我想遍歷登錄,直到一些事件發生(這是在Android長時間運行的服務)。現在我能夠通過保持觀察的一個變量,訂閱,然後取消訂閱並重新訂閱它在它做到這一點的onComplete
obs = whenThisHappens()
.switchMap(i -> {
if (i.isThisThing()) {
return whenThatHappens();
} else {
return nothingHappened();
}
}
.doOnNext(thing -> {
if (thing.isThatThing()) {
log("Got that thing");
} else {
log("Got nothing");
}
})
.doOnComplete(i -> {
obs.unsubscribe();
obs.subscribe();
}
obs.subscribe();
但我有種感覺我做的事情真的錯了這裏。有沒有更好的方法來實現這一點?我看着retry
,但拋出錯誤只是爲了讓它重試似乎與我現在所做的一樣糟糕。
是否isThatThing()getter方法?如果是這樣,則安裝人員應提出通知該財產已更改的事件。在.net中,我們有INotifyPropertyChanged模式/接口。 – Aron
不太熟悉那種模式。我會調查。謝謝。 –
對不起,你的問題不清楚。你提到一個狀態圖,但你的代碼沒有顯示。你能否包含一個你想達到的大理石圖?無論如何,狀態圖通常使用'scan'很容易實現。 –