Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android Room with kotlin value class?

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

like image 484
TootsieRockNRoll Avatar asked Nov 07 '22 00:11

TootsieRockNRoll


1 Answers

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 converting UserId to Int which is basically the same as Int to Int 😭. Someone will probably solve this problem, but if you have to create a TypeConverter for your Inline class then you are still plus one class for the count (multidex). 👎

like image 196
Ryan Payne Avatar answered Nov 11 '22 12:11

Ryan Payne