Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room DAO with inherited interfaces

I have a DAO interface, of which I have multiple implementations and I want one of them to be a Room implementation (Kotlin):

interface BaseDao {
    fun getAll(): Single<List<SomeData>>
    fun insert(data: List<SomeData>)
}

and my Room (RxRoom) interface:

@Dao
interface RxRoomBaseDao: BaseDao {
    @Query("SELECT * FROM some_data")
    override fun getAll(): Single<List<SomeData>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    override fun insert(data: List<SomeData>)
}

It looks like the Room compiler tries to compile the BaseDao instead of the RxRoomBaseDao and complains error: Dao class must be annotated with @Dao and for both methods error: A DAO method can be annotated with only one of the following:Insert,Delete,Query,Update.

I have also tried an abstract class for the RxRoomBaseDao with the same result.

Is this a known bug or limitation of the Room compiler? How can I achieve what I want without polluting my BaseDao with Room's annotations?

like image 972
shelll Avatar asked Dec 07 '17 15:12

shelll


1 Answers

It seems OP has since moved on from Room, but fwiw, it's possible to use generics to solve this problem:

interface BaseDao<T> {
    @Insert
    fun insert(vararg obj: T)
}
@Dao
abstract class DataDao : BaseDao<Data>() {
    @Query("SELECT * FROM Data")
    abstract fun getData(): List<Data>
}

Check out the "Use DAO's inheritance capability" section in this Official Room blog post

like image 145
kip2 Avatar answered Nov 15 '22 15:11

kip2