I'm trying to use a room entity with a value class:
@JvmInline
value class UserToken(val token: String)
and the entity:
@Entity(tableName = TABLE_AUTH_TOKEN)
data class TokenEntity(
@PrimaryKey val id: Int = 0,
val token: UserToken
)
I get the following error:
error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
public final class TokenEntity {
^
is it even possible to use room with value class? I couldn't find anything about this. thanks
See the comment from @CommonsWare. Android does not yet support value classes for Room.
The same holds true for the value classes introduced in kotlin 1.5. The type is not supported.
— Support Inline class in Room entity
Here is a possible explanation according to Kotlin Inline Classes in an Android World.
Looking to solve this you could try and add a
TypeConverter
for your Inline class, but since your Inline class is just the value it wraps when it’s compiled, this doesn’t make much sense and it doesn’t work as you’d expect even if you tried...I’m just guessing it’s because this is a
TypeConverter
convertingUserId
toInt
which is basically the same asInt
toInt
ðŸ˜. Someone will probably solve this problem, but if you have to create aTypeConverter
for your Inline class then you are still plus one class for the count (multidex). 👎
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With