I am trying to persist a timestamp in my room database using the following TypeConverter:
class Converters {
@TypeConverter
fun fromTimestamp(value: Long?): Calendar? {
if(value == null) return null
val cal = GregorianCalendar()
cal.timeInMillis = value
return cal
}
@TypeConverter
fun toTimestamp(timestamp: Calendar?): Long? {
if(timestamp == null) return null
return timestamp.timeInMillis
}
}
Two of my Entities include the following column :
@ColumnInfo(name = "timestamp")
val timestamp: Calendar?,
But I get a compilation error upon trying to build the project - I had no issues when using the Date TypeConverter example from the developer reference guide.
I am unable to see what the actual error is as I just get a bunch of databinding 'cannot find symbol' errors if there is something wrong with the code related to Room.
Use:
object Converters {
@TypeConverter
@JvmStatic
fun fromTimestamp(value: Long?): Calendar? = value?.let { value ->
GregorianCalendar().also { calendar ->
calendar.timeInMillis = value
}
}
@TypeConverter
@JvmStatic
fun toTimestamp(timestamp: Calendar?): Long? = timestamp?.timeInMillis
}
And
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {