简体   繁体   English

在 Worker 上更改数据库后,LiveData 未更新

[英]LiveData not updating after database change on Worker

I have a Worker that send some images to a server.我有一个 Worker 将一些图像发送到服务器。 When the Worker starts, I need to change my view status to "In progress" and when the Worker finish, I need to change my view status to "Finished".当 Worker 启动时,我需要将我的视图状态更改为“进行中”,当 Worker 完成时,我需要将我的视图状态更改为“已完成”。 But, when I update the Room Database inside the Worker, my view can't understand that the database has changed and the LiveData is not notified.但是,当我更新 Worker 内部的Room 数据库时,我的视图无法理解数据库已更改并且未通知LiveData If I make the same call before the Worker starts, the LiveData understand the changes and updates my View.如果我在 Worker 启动之前进行相同的调用, LiveData 会了解更改并更新我的视图。 The problem that I have is that my view doesn't know exactly what item of the recyclerview it should update, so I can't observe the Worker on the View.我遇到的问题是我的视图并不确切知道它应该更新回收视图的哪个项目,所以我无法观察视图上的 Worker。 I need to make this update inside the Worker.我需要在 Worker 内部进行此更新。

--- Updated with code --- --- 更新代码 ---

Fragment分段

viewModel.shortHistoryViewModelList.observe(viewLifecycleOwner, Observer { 
        binding.viewModel = viewModel
        adapter.replace(shortHistories)
        binding.executePendingBindings()
        scrollToItem(shortHistories)
})

ViewModel视图模型

private val results: MediatorLiveData<List<ShortHistory>> = MediatorLiveData()
init {
    this.state.value = NetworkStateEnum.NONE
    results.addSource(repository.get()) {
        results.postValue(it)
    }
}

val shortHistoryViewModelList: LiveData<List<ShortHistoryViewModel>> =
        Transformations.switchMap(results) { shortHistories ->
            val mutableLiveData: MutableLiveData<List<ShortHistoryViewModel>> = MutableLiveData()
            val list = ArrayList<ShortHistoryViewModel>()

            for (shortHistory in shortHistories) {
                list.add(ShortHistoryViewModel(shortHistory))
            }
            mutableLiveData.value = list
            mutableLiveData
        }

Worker工人

val shortHistory = shortHistoryRepository.get(shipmentId)
shortHistory.digitalReceipt = shortHistory.digitalReceipt?.copy(status = DigitalReceiptStatusEnum.Sending.value)
shortHistoryRepository.insert(shortHistory)

UI can only be updated from the UI Thread ( which is also the main Thread ) and not in any other thread like the Worker you are using. UI 只能从 UI 线程(也是主线程)更新,而不能在任何其他线程(如您正在使用的Worker中更新。 Use runOnUiThread(Runnable action) method inside your Worker thread to run any code that has to do with updating the UI.在 Worker 线程中使用runOnUiThread(Runnable action)方法来运行任何与更新 UI 相关的代码。

Not sure what problem you are having since you are refusing to post relevant code but I suspect that you are changing the livedata inside a worker thread.由于您拒绝发布相关代码,因此不确定您遇到了什么问题,但我怀疑您正在更改工作线程内的实时数据。 If that is the case, you need to notify the livedata that it has changed by calling its postValue() method.如果是这种情况,您需要通过调用它的 postValue() 方法来通知 livedata 它已更改。

I went through the same problem.我遇到了同样的问题。

I found that If we run room-db @RawQuery, then liveData or kotlin flow don't get updated.我发现如果我们运行 room-db @RawQuery,那么 liveData 或 kotlin 流不会更新。

So, we have to use normal @Update query instead of @RawQuery.所以,我们必须使用普通的@Update 查询而不是@RawQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM