iex
01/31/2020, 4:17 PMprivate val testSingle = Single.just("item1")
private val foo = testPublishSubject.startWith(testSingle.toObservable().doOnNext {
Timber.i(">>> single on next: $it")
}).doOnNext {
Timber.i(">>> PS on next: $it")
}
fun onSomeEvent() {
testPublishSubject.onNext("item2")
}
So it's a PublishSubject
that gets initialized with a Single
. On certain event, I push a new value to the subject. So far so good!
The problem: I navigate to next screen and navigate back. Then the subject is reset to the initial Single
value ("item1"). Why? The ViewModel
is not reinitialized.stantronic
01/31/2020, 4:34 PMHitender Pannu
01/31/2020, 4:34 PMiex
01/31/2020, 4:35 PMfoo
again. (It's in a fragment and it subscribes in onCreateView
)Single
is fetching data from preferences, and it doesn't re-fetch it when going back for some reason.Single
's result is cached... 🤔Single
is executed againstantronic
01/31/2020, 4:37 PMval
to a fun
iex
01/31/2020, 4:38 PMstantronic
01/31/2020, 4:38 PMiex
01/31/2020, 4:38 PMprivate val foo = testPublishSubject.startWith(myRepo.mySingleCall().toObservable())
PublishSubject
but not calling the Single
again?myRepo.mySingleCall()
when reinitializing the subjectstantronic
01/31/2020, 4:43 PMiex
01/31/2020, 4:44 PMfun selectedModel(): Single<Optional<Model>> =
(preferences.getString(SELECTED_MODEL)?.let {
Some(Model(it))
} ?: None).let{ Single.just(it) }
stantronic
01/31/2020, 4:45 PMiex
01/31/2020, 4:48 PMfun selectedModel() = Single.just("foo")
stantronic
01/31/2020, 5:01 PMSingle.fromCallable { /* fetch from preferences here */ }
iex
01/31/2020, 5:05 PMval
. I posted this snippet above:
private val foo = testPublishSubject.startWith(myRepo.mySingleCall().toObservable())
Single
is not a val
mySingleCall()
is implemented basically like this: fun mySingleCall() = Single.just("foo")