は、次のデータモデル持つ:Androidの反応系プログラミングでは、フィルタを適用した後にストリームオブジェクトデータを分割する方法はありますか?
data class Response(
private val success: Boolean,
private val error: Error,
private val data: Data
)
を私はsuccess == false
またはデータオブジェクト場合success == true
場合、私Observable
ストリーム内エラーオブジェクトを取得したいです。
私は、次の方法を試してみました:
Observable.just(Response(true, Error(), Data()))
.filter { response -> response.success }
.switchIfEmpty { } //Wanted response.error here
.subscribe { response-> } //Wanted response.data here instead of full response.
誰がこれを達成する方法を知っていますか?私は私がif-else
を実装することができますflatMap
の代わりfilter
を使用して、私の目標を達成している
... //observableChain
.subscribe { result: Response ->
when (result) {
is Success -> handleSuccess(result.data)
is Error -> handleError(result.error)
}
}