https://kotlinlang.org logo
#android
Title
# android
m

matt tighe

07/25/2019, 11:32 PM
is it possible to use sealed classes as types for fields of room entities? something like
Copy code
sealed class Animal
object Dog : Animal()
object Cat : Animal()

@Entity
data class Person(
    val name: String
    val favoriteTypeOfAnimal: Animal
)
writing a type converter doesn’t seem to do it
m

Mark Murphy

07/25/2019, 11:39 PM
what is the specific error that you are getting?
m

matt tighe

07/25/2019, 11:41 PM
Type is not directly supported by 'Parcelize'. Annotate the parameter type with '@RawValue' if you want it to be serialized using 'writeValue()'
i figured it out though
need the sealed class to implement `Parcelable`:
Copy code
sealed class Animal : Parcelable
@Parcelize
object Dog : Animal()
@Parcelize
object Cat : Animal()

@Entity
data class Person(
    val name: String
    val favoriteTypeOfAnimal: Animal
)
m

Mark Murphy

07/25/2019, 11:43 PM
that's actually kinda scary, in that
Parcelable
is not meant to be a persistable type
if Room supports
Parcelable
types, some people are going to shoot themselves in the foot (though your scenario may be fine)
j

jw

07/26/2019, 12:08 AM
The error message is from Parcelize and has nothing to do with Room. And Room does not care whether your types are Parcelable or not as it does not use it as a serialization mechanism when targeting the database.
🆗 2
Unless you do something crazy and implement a custom Room adapter for Parcelable... but then it's your foot and your gun so ¯\_(ツ)_/¯
m

matt tighe

07/26/2019, 12:24 AM
hmmm, it seems that was because i had annotated my entity with
@Parcelize
as well. i believe that was so they could be passed in `Bundle`s. is that a bad practice?
j

jw

07/26/2019, 12:27 AM
no
2 Views