I'm experimenting with RxJava. I need an Observable
, which produces a HashSet<String>
. Into Observable
I want to be abele to put Pair<String, Boolean>
in the way that false
boolean value removes the String
key from the resulting HashSet
. Here's a code snippet of what I have:
private val selectionSubject = ReplaySubject.create<Pair<String, Boolean>>()
init {
selectionSubject.onNext(Pair("dd", false))
selectionSubject
.collect({HashSet<String>()}, {dest, value -> collectSelection(dest, value)})
.subscribe { t1, t2 -> Log.d(TAG, t1.toString())}
}
private fun collectSelection(dest: HashSet<String>, value: Pair<String, Boolean>): HashSet<String> {
if (value.second) {
dest.add(value.first)
} else {
dest.remove(value.first)
}
Log.d(TAG, "collectSelection, ${dest.toString()}")
return dest
}
In the logs I can see that collectSelection
gets called but my subscribe listener doesn't.
How can it be fixed?
collect
waits for onComplete
event from the stream above before emitting a value. In your case, ReplaySubject
never ends and thus no value is emitted.
Without knowing the context of selectionSubject
I can't provide a solution to your problem, like, does it have to remain open? If there are limited onNext
calls, you can use .take(X)
. If it has to remain open, you shouldn't depend on collect but add the item inside on HashSet
in something like .doOnNext
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.