Room - LiveData observer does not trigger when database is updated

前端 未结 2 1229
Happy的楠姐
Happy的楠姐 2020-12-03 10:39

I am trying to find out in the code below, why is it that Room\'s LiveData observable does not give me new shifts once I populate the database with new data.

This is

2条回答
  •  無奈伤痛
    2020-12-03 11:02

    In my case, it was because I was using a MediatorLiveData to convert the entities returned from the database and forgot to call setValue() with the converted result, so the mediator was only relying requests to the database but never notifying results.

    override fun getItems() = MediatorLiveData>().apply {
        addSource(itemDao().getItems()) {
            // I was previously only converting the items, without calling 'value ='
            value = it.map(ItemWithTags::toDto)
        }
    }
    

提交回复
热议问题