Room Dao LiveData as return type causing compile time error

后端 未结 4 1758
感情败类
感情败类 2020-12-06 01:12

I am using Room and implemented Dao that returns LiveData. It was working fine with below dependency added.

im         


        
4条回答
  •  情歌与酒
    2020-12-06 01:39

    As Michael Vescovo pointed out, there are two possible ways of achieving async call:

    @Dao
    interface AccountDao{
        @Query("SELECT * FROM account_master")
        suspend fun getAllAccounts(): List
    }
    
    @Dao
    interface AccountDao{
        @Query("SELECT * FROM account_master")
        fun getAllAccounts(): LiveData>
    }
    

    Which one you'll use depends on your use case. For example, I would use the first one if my DAO consumer (usually repository) will create LiveData for the model (in the case that I don't need to observe changes from local DB).

    If I need to observe changes in local DB (for example, some other service can update DB in the meantime) I would use the second one.

提交回复
热议问题