Verified
Status Update
Comments
yb...@google.com <yb...@google.com> #2
do you want to drop all tables or truncate all tables?
i think we can generate a method that does that in RoomDatabase.
i think we can generate a method that does that in RoomDatabase.
je...@gmail.com <je...@gmail.com> #3
I was also looking for this and could not find it. For me it would be ideal if it could drop the whole database. So when the user logs out everything is cleared and the new database is then created on a new log in - the same as if running the application for the first time.
Description
Version used: 1.0.0-alpha1
Devices/Android versions reproduced on: n/a
*** ENTITY ***
@Entity(tableName = "individual")
class Individual constructor() {
@PrimaryKey(autoGenerate = true)
var id: Long = 0
@TypeConverters(DateLongConverter::class)
var lastModified: Date = Date()
}
*** DAO ***
@Dao
interface IndividualDao {
@Query("SELECT lastModified FROM individual WHERE id = :p0")
@TypeConverters(DateLongConverter::class)
fun findLastModified(p0: Long): Date
}
*** Converter ***
class DateLongConverter {
@TypeConverter
fun fromLongToDate(value: Long?): Date? {
value ?: return null
return Date().apply { time = value }
}
@TypeConverter
fun fromDateToLong(value: Date?) = return value?.time
}
*** Generated IndividualDao_Impl (snippit) ***
if(_cursor.moveToFirst()) {
final int _tmp;
_tmp = _cursor.getInt(0);
final String _tmp_1;
_tmp_1 = Integer.toString(_tmp);
final Long _tmp_2;
_tmp_2 = _tmp_1 == null ? null : Long.parseLong(_tmp_1);
_result = __dateLongConverter.fromLongToDate(_tmp_2);
}
The results for individualDao.findLastModified(1):
EXPECTED RESULTS: "Thu May 25 14:36:04 MDT 2017"
ACTUAL RESULTS: "Tue Jan 13 09:25:45 MST 1970"
Observations
1. The generated code does a cursor.getInt(0) instead of a cursor.getLong(0)
2. The code then converts the int to a String, then a String to a Long
3. Why do I need to specify a converter on the Dao function if I defined the converter already in the Entity? (I get a an error during compile if I don't provide a "@TypeConverters(DateLongConverter::class)" on the Dao function)