Verified
Status Update
Comments
yb...@google.com <yb...@google.com> #2
since these are in public API (:/) we need to do this in 1.2
je...@gmail.com <je...@gmail.com> #3
since it is already marked as deprecated, we can probably do it by now.
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)