Search code examples
androidkotlinandroid-roomandroidx

Room database get Set of type


I need to get set of strings from RoomDatabase

Entity:

@Entity
data class Data(
@PrimaryKey val id: Int,
val type: String,
val photo: String
)

I need to get set of types. I've try follow method:

...
@Query("SELECT type FROM data")
fun getAllTypes(): LiveData<Set<String>>
...

Now I has next error:

C:\...\storage\DataDao.java:24: error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.LiveData<java.util.Set<java.lang.String>>).
    public abstract androidx.lifecycle.LiveData<java.util.Set<java.lang.String>> getAllTypes();
[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC).

> Task :app:kaptDebugKotlin FAILED

Solution

  • You can't get Set, sorry. According to documentation for SELECT queries you can use only List or Array.

    https://developer.android.com/reference/androidx/room/Query

    If you need to have unique items you can use SQL's DISTINCT.