Room @Transaction 不适用于 Flowable

Room @Transaction not working with Flowable

我在数据源方法上使用 @Transaction 注释时遇到问题。看起来这个注释在我的情况下不起作用。

我有一个常用的 DAO 房间数据库

@Database(entities = [MyEntity::class], version = 1)
abstract class MyDatabase : RoomDatabase() {
    abstract fun myDao(): MyDao
}

@Dao
interface myDao {
    @Insert
    fun insertAll(list: List<MyEntity>)
    
    @Query("SELECT * FROM MyEntity")
    fun observe(): Flowable<List<MyEntity>>

    @Query("DELETE FROM MyEntity")
    fun clear()
}

当我一直执行 clear()insertAll()

时,我的数据源出现问题
class MyDataSource(database: MyDatabase) {
    private val myDao = database.myDao()

    @Transaction
    fun updateMyEntity(items: List<MyEntity>) {
        myDao.clear()
        myDao.insertAll(items)
    }

    fun observeMyEntity(): Flowable<List<MyEntity>> {
        return myDao.observe()
    }
}

当我调用 updateMyEntity() 时,我在 oberveMyEntity() 上的订阅中得到了两个结果。第一个结果是空列表,第二个是插入列表。当我向方法 updateMyEntity() 添加注释 @Transaction 时,我希望只得到插入的列表。这个注释在我的情况下不起作用吗?

我在文档中看到了以下信息,但我不确定是不是我的情况。

If the query is asynchronous (e.g. returns a {@link androidx.lifecycle.LiveData LiveData} or RxJava {@code Flowable}), the transaction is properly handled when the query is run, not when the method is called.

Putting this annotation on an {@link Insert}, {@link Update} or {@link Delete} method has no impact because those methods are always run inside a transaction. Similarly, if a method is annotated with {@link Query} but runs an INSERT, UPDATE or DELETE statement, it is automatically wrapped in a transaction and this annotation has no effect.

如果将 database 设为 属性 并用 database.runInTransaction { } 包装调用 clear()insertAll(),问题就会消失。

Transaction 只能用于 Dao 方法