Я переношу свой код в AndroidX, и внезапно я получаю исключение IllegalStateException: Cannot invoke observeForever on a background thread
.
Все вопросы, которые я нашел здесь, касались использования наблюдать за Forever, но я ничего не нашел о addSource. Какой обходной путь я могу сделать, чтобы мой код работал нормально?
observationRepository.allUserObservationsForMonitoring(monitoringId)?.let {
observationListLiveData.addSource(it) { #EXCEPTION
observationListLiveData.postValue(it)
}
}
fun allUserObservationsForMonitoring(monitoringId: Long): LiveData<List<Observation>>? {
return observationDao?.getUserObservationsForMonitoring(monitoringId)
}
Объект наблюдениеListLiveData является MediatorLiveData.
РЕДАКТИРОВАТЬ: обновление с журналом ошибок
2020-02-17 11:03:26.945 30126-30252/io.b4c.brain4care.qa E/BLEBC: 17/02/2020 11:03:26.942 - Error creating monitoring
java.lang.IllegalStateException: Cannot invoke observeForever on a background thread
at androidx.lifecycle.LiveData.assertMainThread(LiveData.java:462)
at androidx.lifecycle.LiveData.observeForever(LiveData.java:222)
at androidx.lifecycle.MediatorLiveData$Source.plug(MediatorLiveData.java:141)
at androidx.lifecycle.MediatorLiveData.addSource(MediatorLiveData.java:96)
at io.b4c.brain4care.modules.monitoring.MonitoringViewModel.setupMonitoringObservations(MonitoringViewModel.kt:131)
at io.b4c.brain4care.modules.monitoring.MonitoringViewModel.access$setupMonitoringObservations(MonitoringViewModel.kt:34)
at io.b4c.brain4care.modules.monitoring.MonitoringViewModel$startMonitoring$1.invoke(MonitoringViewModel.kt:258)
at io.b4c.brain4care.modules.monitoring.MonitoringViewModel$startMonitoring$1.invoke(MonitoringViewModel.kt:34)
at io.b4c.brain4care.util.repositories.MonitoringRepository$retryUpsertMonitoring$subscription$2.call(MonitoringRepository.kt:104)
at io.b4c.brain4care.util.repositories.MonitoringRepository$retryUpsertMonitoring$subscription$2.call(MonitoringRepository.kt:18)
at rx.internal.util.ActionSubscriber.onNext(ActionSubscriber.java:39)
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:134)
at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.onNext(OperatorSubscribeOn.java:74)
at rx.internal.producers.SingleDelayedProducer.emit(SingleDelayedProducer.java:102)
at rx.internal.producers.SingleDelayedProducer.setValue(SingleDelayedProducer.java:85)
at rx.internal.operators.OnSubscribeFromCallable.call(OnSubscribeFromCallable.java:48)
at rx.internal.operators.OnSubscribeFromCallable.call(OnSubscribeFromCallable.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10256)
at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100)
at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:230)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:459)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
2020-02-17 11:03:26.948 30126-30252/io.b4c.brain4care.qa E/BLEBC: 17/02/2020 11:03:26.945 - Error saving monitoring in DB
java.lang.IllegalStateException: Cannot invoke observeForever on a background thread
at androidx.lifecycle.LiveData.assertMainThread(LiveData.java:462)
at androidx.lifecycle.LiveData.observeForever(LiveData.java:222)
at androidx.lifecycle.MediatorLiveData$Source.plug(MediatorLiveData.java:141)
at androidx.lifecycle.MediatorLiveData.addSource(MediatorLiveData.java:96)
at io.b4c.brain4care.modules.monitoring.MonitoringViewModel.setupMonitoringObservations(MonitoringViewModel.kt:131)
at io.b4c.brain4care.modules.monitoring.MonitoringViewModel.access$setupMonitoringObservations(MonitoringViewModel.kt:34)
at io.b4c.brain4care.modules.monitoring.MonitoringViewModel$startMonitoring$1.invoke(MonitoringViewModel.kt:258)
at io.b4c.brain4care.modules.monitoring.MonitoringViewModel$startMonitoring$1.invoke(MonitoringViewModel.kt:34)
at io.b4c.brain4care.util.repositories.MonitoringRepository$retryUpsertMonitoring$subscription$2.call(MonitoringRepository.kt:104)
at io.b4c.brain4care.util.repositories.MonitoringRepository$retryUpsertMonitoring$subscription$2.call(MonitoringRepository.kt:18)
at rx.internal.util.ActionSubscriber.onNext(ActionSubscriber.java:39)
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:134)
at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.onNext(OperatorSubscribeOn.java:74)
at rx.internal.producers.SingleDelayedProducer.emit(SingleDelayedProducer.java:102)
at rx.internal.producers.SingleDelayedProducer.setValue(SingleDelayedProducer.java:85)
at rx.internal.operators.OnSubscribeFromCallable.call(OnSubscribeFromCallable.java:48)
at rx.internal.operators.OnSubscribeFromCallable.call(OnSubscribeFromCallable.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10256)
at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100)
at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:230)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:459)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)