Room Flow not triggered on delete when using @RawQuery

38 views Asked by At

I am listening for changes in my local database with

@Query("SELECT * FROM favorite_recipes_table ORDER BY id ASC")
fun readFavoriteRecipesAsFlow(): Flow<List<FavoriteEntity>>

But when I run another method in my DAO to delete items in the same table:

@RawQuery
suspend fun deleteFavoritesRecipe(query: SupportSQLiteQuery): Int

The flow returned from readFavoriteRecipesAsFlow is not called. What could be the problem?

1

There are 1 answers

0
Artenes Nogueira On

This happens because @RawQuery was used.

According to this issue: https://issuetracker.google.com/issues/235373409

The delete function in the sample project actually uses a @RawQuery method which does not have info on the type of operation being done, hence does not know it's a WRITE. This results in a transaction not being run which means invalidation is never kicked off

A workaround is to add @Transaction in the query method:

@RawQuery
@Transaction
suspend fun deleteFavoritesRecipe(query: SupportSQLiteQuery): Int

Or you can rethink how you need to make this query to use simple annotations such as @Query, @Delete, etc.

An issue was opened requesting this feature to notify for changes when using @RawQuery at https://issuetracker.google.com/issues/235878346, but no updates were made from the past 2 years.