I have two tables, Location
and Weather
. They have one-to-one relationship. Weather
has a foreign key that keeps the id
of corresponding entry in Location
.
@Entity(tableName = "location")
data class Location(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
...
)
@Entity(
tableName = "weather",
foreignKeys = [
ForeignKey(
entity = Location::class,
parentColumns = ["id"],
childColumns = ["locationId"],
onDelete = ForeignKey.CASCADE
)
]
)
data class Weather(
@PrimaryKey(autoGenerate = true) val id: Int = 0,
@ColumnInfo(name = "locationId") val locationId: Long,
...
)
Then I have this composite class:
data class LocationWeather(
@Embedded val location: Location,
@Relation(
parentColumn = "id",
entityColumn = "locationId"
)
val weather: Weather
)
I want to return a flow of list of LocationWeather
so that when any of the tables are updated, I expect a new value to be emitted. Here is the DAO method:
@Transaction
@Query("SELECT * FROM location")
fun getLocationWeatherList(): Flow<List<LocationWeather>>
Then, I directly call DAO method in my ViewModel (just for now, there will be domain and data layers in between) to test it:
locationDao.getLocationWeatherList().onEach {
// Only triggered once...
}.launchIn(viewModelScope)
However, I get data only once. There are no other emits after any of Location
or Weather
is updated, even though database inspector shows new entries.
I followed the official docs. The only difference is that, in the DAO method, instead of returning List<LocationWeather>
, I return Flow<List<LocationWeather>>
.
Is this expected or am I missing something?