Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room TypeConverter with constructer

I have Room TypeConverter and I need to inject parameter to it's constructor

class RoomConverters(moshi Moshi) {

    @TypeConverter
    fun fromUserActionLog(data: UserActionLogModel): String {
        return moshi.adapter(UserActionLogModel::class.java).toJson(data)
    }

    @TypeConverter
    fun toUserActionLog(json: String): UserActionLogModel {
        return moshi.adapter(UserActionLogModel::class.java).fromJson(json)}
    }
}

But when I can not annotate TypeConverter to database object with contructor;

@Database(entities = [SsidModel::class], version = 1, exportSchema = false)
@TypeConverters(RoomConverters::class)   
abstract class AppDatabase : RoomDatabase() {

    abstract fun ssidDao(): SsidDao
}

Is there any way to achieve this?

like image 414
Murat Avatar asked Nov 02 '18 07:11

Murat


2 Answers

Now that 2.3.0 of Room library is released. It is possible to instantiate Room type converters and provide them to database builder.

Add @ProvidedTypeConverter annotation to the TypeConverter class.

@ProvidedTypeConverter
class RoomConverter(moshi Moshi) {

    @TypeConverter
    fun fromUserActionLog(data: UserActionLogModel): String {
        return moshi.adapter(UserActionLogModel::class.java).toJson(data)
    }

    @TypeConverter
    fun toUserActionLog(json: String): UserActionLogModel {
        return moshi.adapter(UserActionLogModel::class.java).fromJson(json)}
    }
}

Mention the TypeConverter in the @TypeConverter annotation of the database abstract class.

@Database(entities = [/* entities here */], version = 1, exportSchema = false)
@TypeConverters(RoomConverters::class)   
abstract class AppDatabase : RoomDatabase() {
    ......
    // your code here
    ......
}

Now build the database using the static method databaseBuilder of Room class and provide TypeConverter using the method addTypeConverter()

val roomConverter = RoomConverter(moshi)
val appDb = Room.databaseBuilder(
                            applicationContext,
                            AppDatabase::class.java, DB_NAME
                        )
                            .addTypeConverter(roomConverter)
                            .build()
like image 96
Nataraj KR Avatar answered Sep 19 '22 07:09

Nataraj KR


You can create Room TypeConverter with constructor parameters from version 2.3.0-alpha03

Release notes:

Room now has APIs for providing instances of type converters such that the app can control their initialization. To mark a type converter that will be provided to Room use the new annotation @ProvidedTypeConverter

https://developer.android.com/jetpack/androidx/releases/room#2.3.0-alpha03

In your case you should add @ProvidedTypeConverter to RoomConverter

@ProvidedTypeConverter    
class RoomConverters(moshi: Moshi)

Create converter at db creation time and pass it to database builder:

val roomConverter = RoomConverters(Moshi())
val db = Room.databaseBuilder()
         .addTypeConverter(roomConverter)
         .build()

Also you can use DI framework e.g. Dagger2

like image 36
m.myalkin Avatar answered Sep 20 '22 07:09

m.myalkin