is it possible to use sealed classes as types for ...
# android
m
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
what is the specific error that you are getting?
m
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
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
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
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
no