简体   繁体   English

在多个进程中获取 LiveData 更新

[英]Getting LiveData updates in multiple processes

In my Android application I use the Room API to persist data that comes from the network.在我的 Android 应用程序中,我使用 Room API 来保存来自网络的数据。 The app is split into several processes, with one of them responsible for syncing the data (implementing SyncAdapter).该应用程序分为多个进程,其中一个进程负责同步数据(实现 SyncAdapter)。 The UI is running on a different process, with it's own DAO to access the DB. UI 在不同的进程上运行,它有自己的 DAO 来访问数据库。 My issue is that the LiveData I query on the UI process, doesn't get any updates when the SyncAdapter writes new data to the DB.我的问题是,当 SyncAdapter 将新数据写入数据库时,我在 UI 进程中查询的 LiveData 没有得到任何更新。 Because the DB is shared between processes, I expected the OnChanged to be called on all processes that have LiveData that reflects the DB, meaning that all processes can observe data base changes因为数据库在进程之间共享,所以我希望在所有具有反映数据库的 LiveData 的进程上调用 OnChanged,这意味着所有进程都可以观察数据库更改

enableMultiInstanceInvalidation introduced in room 2.1.0 may help you official docs room 2.1.0 中引入的 enableMultiInstanceInvalidation 可能对你有帮助官方文档

You will need to implement your own IPC mechanism to pass events from process to process.您将需要实现自己的 IPC 机制以将事件从一个进程传递到另一个进程。 This is not significantly different than using SQLite directly, or using most other observer frameworks (eg, data binding observables).这与直接使用 SQLite 或使用大多数其他观察器框架(例如,数据绑定可观察对象)没有明显不同。

Or, switch to having a single process, in which case your existing implementation may work just fine.或者,切换到单一进程,在这种情况下,您现有的实现可能会工作得很好。

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

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