'Android Room: Particular DAO Method Not Returning Data

I have this particular "get" method that isn't returning data but I have another, more complex, "get" method that works fine.

The one in question is super simple and I have no idea why it doesn't work. What's dumb is if I use the Database Inspector and run that exact SQL query on the table, it'll return all the rows in the table.

Table query

Here's the DAO method:

@Query(
        "SELECT c._id, " +
                  "c.name, " +
                  "c.notes " +
              "FROM caliber c"
    )
    fun getCalibers(): Flow<List<Caliber>>

The "get" DAO method that does work also returns a Flow<List<MyOtherObject>> and is right now being used to populate a LazyColumn list of items via a ViewModel.

My dropdown list powered by this problematic DAO query was blank and I finally got a test method setup to test the DAO method directly (ignoring the Repository, which just calls the DAO anyway in this case):

@RunWith(AndroidJUnit4::class)
@SmallTest
class MyDaoTest {
    private lateinit var database: MyDatabase
    private lateinit var dao: MyDao

    @Before
    fun setup() {
        database = Room.inMemoryDatabaseBuilder(
            ApplicationProvider.getApplicationContext(),
            MyDatabase::class.java
        )
            .allowMainThreadQueries()
            .addMigrations(DatabaseMigration.migration1To2)
            .addMigrations(DatabaseMigration.migration2To3)
            .addMigrations(DatabaseMigration.migration3To4)
            .addMigrations(DatabaseMigration.migration4To5)
            .build()

        dao = database.myDao
    }

    @After
    fun teardown() {
        database.close()
    }

    @Test
    fun calibersArePopulated(): Unit = runBlocking {
        val muhCalibers = dao.getCalibers().first()

        assertThat(muhCalibers).isNotEmpty()
    }
}

The database build call is copied and pasted right from my Dagger/Hilt "provideDatabase" function, with the addition of allowMainThreadQueries() here and the exclusion of the db name since it's in-memory. That test above is failing (I debugged the assertThat line and the size/count was 0 for the list).

Here's the Entity I'm using. I just now removed all of the Flow stuff from all of the calls in my app, and just tried returning List from the DAO, but same problem.

@Entity(tableName = "caliber")
class Caliber {
    @ColumnInfo(name = "_id")
    @PrimaryKey(autoGenerate = true)
    var id = 0

    var name: String = ""

    var notes: String? = null

    @Ignore
    constructor() {
    }

    @Ignore
    constructor(
        name: String,
        notes: String?
    ) {
        this.name = name
        this.notes = notes
    }

    constructor(
        id: Int,
        name: String,
        notes: String?
    ) {
        this.id = id
        this.name = name
        this.notes = notes
    }
}

Any ideas what's wrong here? Getting kinda frustrated at this thing. :-/



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source