Last active
January 12, 2023 13:44
-
-
Save tinmegali/d4a477785f01e57066915a44543db6ed to your computer and use it in GitHub Desktop.
Android Room @TypeConverter using Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Database(entities = arrayOf(Note::class, User::class), version = 1) | |
@TypeConverters(Converters::class) | |
abstract class AppDatabse : RoomDatabase() { | |
abstract fun userDAO(): UserDAO | |
abstract fun noteDAO(): NoteDAO | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tinmegali.daggerwithkotlin.room | |
import android.arch.persistence.room.TypeConverter | |
import java.util.Date | |
class Converters { | |
@TypeConverter | |
fun fromTimestamp(value: Long?): Date? { | |
return if (value == null) null else Date(value) | |
} | |
@TypeConverter | |
fun dateToTimestamp(date: Date?): Long? { | |
return date?.time | |
} | |
} |
to use*
For some reason converting kotlin.time.Duration
does not work but java.time.Duration
Are there a generic Converter for enum? Or do we need write specific converter for each enum that used?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I create a converter of my own Object. Let us say I have an User object and I want to add this entity to the database
@entity
data class Users(val orderId: UUID, val orderDate: Date, val manager: Manager, val office: Office) {}
I have the Manager class and the Office class that I want to us a converter, how do I go about that?
Thanks