Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room Dao LiveData as return type causing compile time error

Tags:

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

implementation "androidx.room:room-runtime:2.1.0-alpha04" kapt "androidx.room:room-compiler:2.1.0-alpha04" 

But when I added new Room coroutine dependency as mentioned below.

implementation "androidx.room:room-runtime:2.1.0-alpha04" implementation "androidx.room:room-coroutines:2.1.0-alpha04" kapt "androidx.room:room-compiler:2.1.0-alpha04" 

Below is code which compiles

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

Below is the code which gives error.

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

started to receive error.

PlayGround/app/build/tmp/kapt3/stubs/debug/com/playground/www/x/datasource/dao/AccountDao.java:11: error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.LiveData<java.util.List<com.playground.www.x.datasource.entity.Account>>). public abstract java.lang.Object getAllAccounts(@org.jetbrains.annotations.NotNull() 

Any one facing similar issue?

like image 208
Pinakin Kansara Avatar asked Feb 07 '19 05:02

Pinakin Kansara


People also ask

What is DAO in Room database?

When you use the Room persistence library to store your app's data, you interact with the stored data by defining data access objects, or DAOs. Each DAO includes methods that offer abstract access to your app's database. At compile time, Room automatically generates implementations of the DAOs that you define.

What is DAO in android?

Data Access Objects are the main classes where you define your database interactions. They can include a variety of query methods. The class marked with @Dao should either be an interface or an abstract class.


2 Answers

I think the solution here is actually to just return the LiveData without using Coroutines. LiveData works out of the box, there's no reason to use Coroutines when returning LiveData.

When using LiveData it already handles it on a background thread. When NOT using LiveData then in that case you can use Coroutines (and maybe eventually Coroutines Channels) or RxJava2.

See this codelab for an example: https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin. Here they need a background thread for inserts but not for the returned LiveData.

Note: there seems to be a mistake in the actual codelab where the DAO is not returning a LiveData. I've corrected that in the sample below.

@Dao interface WordDao {      @Query("SELECT * from word_table ORDER BY word ASC")     fun getAllWords(): LiveData<List<Word>>      @Insert     suspend fun insert(word: Word)      @Query("DELETE FROM word_table")     fun deleteAll() }  class WordRepository(private val wordDao: WordDao) {      val allWords: LiveData<List<Word>> = wordDao.getAllWords()      @WorkerThread     suspend fun insert(word: Word) {         wordDao.insert(word)     } } 
like image 96
Michael Vescovo Avatar answered Oct 13 '22 00:10

Michael Vescovo


Remove the suspend function. LiveData is already asynchronous. No need for a suspend function.

@Dao interface AccountDao{      @Query("SELECT * FROM account_master")     fun getAllAccounts(): LiveData<List<Account>> } 
like image 33
mitch Avatar answered Oct 13 '22 01:10

mitch