本文介绍了从LifeCycleService观察LiveData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用google android体系结构组件,并且有一个前台服务,即LifeCycleService.

I'm using the google android architecture component and I have a foreground service which is a LifeCycleService.

问题是当我尝试从存储库中观察LiveData时,可观察到的onChanged不会触发.如果我使用observeForever而不是observe,它会触发.

The problem is when i try to observe a LiveData from my repository, the observable onChanged doesn't trigger. If I use observeForever instead of observe it does trigger.

什么会引起这样的问题?

What could cause such a problem?

推荐答案

原来,我是在不调用super.onBind的情况下重写onBind的,而super.onBind并未触发LifeCycleService处于STARTED状态.这意味着我的观察者总是不活跃

Turns out I was overriding the onBind without calling super.onBind which didn't trigger the LifeCycleService to be in a STARTED state. Which meant that my observer was always inactive

这篇关于从LifeCycleService观察LiveData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 20:14