Using Room’s @ForeignKey as @Entity parameter in Kotlin

This is the way to provide the annotation you’re looking for, with explicit arrays for the arguments, and no @ for the nested annotation’s creation: @Entity(tableName = “Foo”, foreignKeys = arrayOf( ForeignKey(entity = Bar::class, parentColumns = arrayOf(“someCol”), childColumns = arrayOf(“someOtherCol”), onDelete = CASCADE))) Since Kotlin 1.2, you can also use array literals: @Entity(tableName = “Foo”, … Read more

Room Using Date field

You’re converting from Date to Long (wrapper) and from long (primitive) to Date. I changed it to Long and it compiled. Besides, unboxing null in your converter produces a NPE. public class DateConverter { @TypeConverter public static Date toDate(Long dateLong){ return dateLong == null ? null: new Date(dateLong); } @TypeConverter public static Long fromDate(Date date){ … Read more

How to save enum field in the database room?

You can make a convert to each enum, like this: class Converters { @TypeConverter fun toHealth(value: String) = enumValueOf<Health>(value) @TypeConverter fun fromHealth(value: Health) = value.name } Or if you prefer store it as SQL integer, you can use ordinal too: class Converters { @TypeConverter fun toHealth(value: Int) = enumValues<Health>()[value] @TypeConverter fun fromHealth(value: Health) = value.ordinal … Read more

Please provide a Migration in the builder or call fallbackToDestructiveMigration in the builder in which case Room will re-create all of the tables

If you don’t want to provide migrations and you specifically want your database to be cleared when you upgrade the version, call fallbackToDestructiveMigration in the database builder database = Room.databaseBuilder(context.getApplicationContext(), UsersDatabase.class, “Sample.db”) .fallbackToDestructiveMigration() .build();

How to import Room Persistence Library to an Android project

It’s possible to find the dependencies on the example codelab for the new architecture components. Root : allprojects { repositories { jcenter() maven { url “https://maven.google.com” } } For Room: implementation ‘android.arch.persistence.room:runtime:1.0.0-alpha1’ annotationProcessor ‘android.arch.persistence.room:compiler:1.0.0-alpha1’ For Lifecycle dependencies: implementation ‘android.arch.lifecycle:extensions:1.0.0-alpha1’ annotationProcessor ‘android.arch.lifecycle:compiler:1.0.0-alpha1’ Adding Rxjava2 objects as result for our queries: implementation ‘android.arch.persistence.room:rxjava2:1.0.0-alpha1′ Test migrations: testImplementation’android.arch.persistence.room:testing:1.0.0-alpha1’

How to dynamically query the room database at runtime?

Room supports @RawQuery annotation to construct queries at run-time. Step 1 : Make DAO method Mark the DAO method with @RawQuery annotation instead of normal @Query. @Dao interface BooksDao{ @RawQuery List<Book> getBooks(SupportSQLiteQuery query); } Step 2 : Construct the query Room uses prepared statements for security and compile time verification. Therefore, while constructing queries, we … Read more

How to combine two live data one after the other?

You can use my helper method: val profile = MutableLiveData<ProfileData>() val user = MutableLiveData<CurrentUser>() val title = profile.combineWith(user) { profile, user -> “${profile.job} ${user.name}” } fun <T, K, R> LiveData<T>.combineWith( liveData: LiveData<K>, block: (T?, K?) -> R ): LiveData<R> { val result = MediatorLiveData<R>() result.addSource(this) { result.value = block(this.value, liveData.value) } result.addSource(liveData) { result.value = … Read more