1

I have a database that has a table with multiple columns, and I wish to only load 3 when showing my list and before the user selects one item to show it.

In order to do this I have created the following dao

@Dao
interface ReportDao {
    @Query("SELECT id, name, description, created FROM reports ORDER BY created DESC")
    fun loadShortReports(): LiveData<List<ReportItemDO>>

    @Insert(onConflict = OnConflictStrategy.FAIL)
    fun saveReports(reports: List<ReportDO>): Completable
 }

this is my report entity:

@Entity(tableName = "reports")
data class ReportDO(
    @PrimaryKey val id: UUID
    , val name: String
    , val description: String
     .......)

and this is the minified entity for the list

data class ReportItemDO(
     override val id: UUID
     , override val name: String
     , override val description: String
     , @ColumnInfo(name = "created") override val date: Date)
     : ReportItemVO

(the ReportItemVO interface is what the list expects)

When the application loads, everything loads correctly

but in order to test this I created a simple button that adds a random ReportDO item to the database using this code:

repository.addReport(
        report
    ).subscribeOn(schedulers.subscribeScheduler)
        .observeOn(schedulers.observeScheduler)
        .subscribe {
            Log.i(MainViewModel.TAG, "success")
        }

which in turn calls

reportDao.saveReports(listof(report))

but the list does not seem to get updated automatically

When I close and restart the application the list is updated

I am assuming this is because I'm only taking a subset of the entity so Room does not know it should trigger the update

Is there a way to make Room understand that the LiveData it created was updated and trigger the loading process again?

Also , because I wish to add a refresh button, is there a way to send Room a message to manually reload the data?

Cruces
  • 3,029
  • 1
  • 26
  • 55
  • Have you verified that your database and DAO are both singleton through the whole app? [See this answer](https://stackoverflow.com/a/44958478/3891038) just in case. – Julio E. Rodríguez Cabañas May 07 '19 at 16:20
  • Yes, the dao is set as `abstract val reportDao: ReportDao` in the database class, and I initialize the database in Application's on create, then I save it in its companion object, whenever I inject the database (or the dao) though a constructor I pass the reference to MyApplication.database or MyApplication.database.reportDao – Cruces May 07 '19 at 16:30
  • Room should still refresh even if you don't select all columns. Try `Select * ...` and see if refresh. – Sanlok Lee May 07 '19 at 20:14

0 Answers0