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
)iex
01/31/2020, 4:35 PMSingle
is fetching data from preferences, and it doesn't re-fetch it when going back for some reason.iex
01/31/2020, 4:37 PMSingle
's result is cached... 🤔iex
01/31/2020, 4:37 PMSingle
is executed againstantronic
01/31/2020, 4:37 PMstantronic
01/31/2020, 4:38 PMval
to a fun
iex
01/31/2020, 4:38 PMstantronic
01/31/2020, 4:38 PMiex
01/31/2020, 4:38 PMiex
01/31/2020, 4:39 PMiex
01/31/2020, 4:40 PMiex
01/31/2020, 4:40 PMiex
01/31/2020, 4:42 PMprivate val foo = testPublishSubject.startWith(myRepo.mySingleCall().toObservable())
iex
01/31/2020, 4:43 PMPublishSubject
but not calling the Single
again?iex
01/31/2020, 4:43 PMmyRepo.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) }
iex
01/31/2020, 4:44 PMstantronic
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 */ }
stantronic
01/31/2020, 5:01 PMiex
01/31/2020, 5:05 PMval
. I posted this snippet above:
private val foo = testPublishSubject.startWith(myRepo.mySingleCall().toObservable())
iex
01/31/2020, 5:05 PMSingle
is not a val
iex
01/31/2020, 5:07 PMmySingleCall()
is implemented basically like this: fun mySingleCall() = Single.just("foo")
iex
01/31/2020, 5:07 PM