【问题标题】:How can I read constantly updated value from external database with room?如何从带有房间的外部数据库中读取不断更新的值?
【发布时间】:2018-06-08 02:36:51
【问题描述】:

我使用带有 Live Data 的空间来访问外部数据库。 存储在外部数据库中的值由另一个应用程序每 5 秒更新一次。 似乎我无法不断地从房间接收更新的值。 我应该重建数据库以每 5 秒更新一次值吗?

这里有一些参考代码:

@Dao public interface GasDynamicDao {

@Query("Select * from gas_dyamic")
LiveData<List<GasDynamicEntity>> getAllInformation();

@Query("Select * from gas_dyamic")
List<GasDynamicEntity> getAllInformationSync();

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<GasDynamicEntity> products);

@Query("Select * from gas_dyamic where Device_ID = :deviceID")
LiveData<GasDynamicEntity> loadGas(String deviceID);
@Query("Select * from gas_dyamic where Device_ID = :deviceID")
GasDynamicEntity loadGasSync(String deviceID);

}

public static GasDatabase buildDatabase(final Context appContext, final AppExecutors executors) {
    return Room.databaseBuilder(appContext, GasDatabase.class,
            DATABASE_FULL_PATH).allowMainThreadQueries().build();
}

【问题讨论】:

  • 另一个应用程序对我来说是黑匣子。我无法修改代码。
  • 我只知道它更改的数据库的文件路径和有关其格式的文档。我想尝试使用 Room + LiveData 来替换 ContentProvider。
  • 我不知道 "other app" 的实现。我想使用新技术(android 架构组件)来读取数据库并在可能的情况下显示它们。

标签: android database android-room android-livedata


【解决方案1】:

我通过轮询数据库来解决问题。 但是数据库不需要从房间重新创建或重新加载。 LiveData 将通过 Dao 方法重新查询数据库再次发生变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-17
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 2022-01-23
    相关资源
    最近更新 更多