Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

suspend method inside runInTransaction block

I have a compilation error using the code below:

Suspension functions can be called only within coroutine body

Can someone explain to me why? What do I need to do to make it work (without using the @Transaction annotation)?

override suspend fun replaceAccounts(newAccounts: List<Account>) {
    database.runInTransaction {
        database.accountDao().deleteAllAccounts() // I have the error on this line
        database.accountDao().insertAccounts(newAccounts) // Here too
    }
}

@Dao
abstract class AccountDao : BaseDao<AccountEntity> {

    @Query("DELETE FROM Account")
    abstract suspend fun deleteAllAccounts()

}

Thanks in advance for your help

like image 645
Louis Avatar asked Aug 31 '20 19:08

Louis


Video Answer


1 Answers

For suspend functions you should use withTransaction instead of runInTransaction

like image 155
IR42 Avatar answered Oct 20 '22 14:10

IR42