Reputation: 2278
Imagine we have one to many relationship. Two tables, one is parent_table and another is child_table. Two entity classes called ParentEntity and ChildEntity. And one embeded class called ParentWithChild using it on join query result.
ParentEntity Class
@Entity(tableName = "parent_table")
data class ChildEntity(
@ColumnInfo(name = "parent_id")
@PrimaryKey(autoGenerate = false)
val id: String,
@ColumnInfo(name = "parent_name")
val name: String,
)
ChildEntity Class
@Entity(
tableName = "child_table",
foreignKeys = [ForeignKey(
entity = ParentEntity::class,
parentColumns = ["parent_id"],
childColumns = ["parent_owner_id"],
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE,
deferred = false
)])
data class ChildEntity(
@ColumnInfo(name = "child_id")
@PrimaryKey(autoGenerate = false)
val id: String,
@ColumnInfo(name = "parent_owner_id")
val childId: String,
@ColumnInfo(name = "child_name")
val name: String,
)
ParentWithChild Class
data class ParentWithChild(
@Embedded
val parent: ParentEntity?,
@Embedded
val child : ChildEntity?,
)
And the sample query in dao class is
@Query("""
select *
from parent_table left join child_table on parent_table.parent_id = child_table.parent_owner_id
""")
abstract fun loadParentsWithChildren(): Flow<List<ParentWithChild>>
Now two scenarios come to play, if we use loadParentsWithChildren() mehtod.
1- If parent table is updated the trigger happens and the new data stream emitted by room.
2- If the child table is updated the room does not re fetch the join query statements.
My problem is the second scenario, what am I doing wrong ?
Thanks in advance.
Upvotes: 4
Views: 498
Reputation: 2278
By adding enableMultiInstanceInvalidation() when creating room database, my problem solved.
@InstallIn(SingletonComponent::class)
@Module
class DatabaseModule {
@Provides
fun provideDatabase(@ApplicationContext context: Context): AppDatabase {
return Room
.databaseBuilder(context, AppDatabase::class.java, "app.db")
.fallbackToDestructiveMigration()
.enableMultiInstanceInvalidation()
.build()
}
}
But still I wonder that how I'm using different database instance since I'm using Hilt library and following singletone pattern, and I'm also not using database in multi process ...
Upvotes: 5