I have this AsyncSubject
val asyncSubject = AsyncSubject.create<Int>()
observable.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.newThread())
.subscribe(asyncSubject)
If I do something like this in test environment (androidTest folder)
asyncSubject.subscribe{
// enter only in test environment
}
it enters only during testing, but I ran the same code in an activity it doesn't enter.
The only way to made it work both test and normal environment is to remove:
.observeOn(Schedulers.newThread())
.subscribeOn(AndroidSchedulers.mainThread())
What I am doing wrong?
Those are my dependencies:
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
EDIT:
it appears that it doesn't work only if I try to change an outside variable. An example:
val observable = Observable.just(1, 2, 3, 4, 5)
val asyncSubject = AsyncSubject.create<Int>()
observable.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(asyncSubject)
val signal = CountDownLatch(5)
var i = 0
asyncSubject.subscribe{
i = it
signal.countDown()
}
signal.await()
// After that `i` is always 0