Verified
Status Update
Comments
yb...@google.com <yb...@google.com> #2
hmm that is weird that we are converting to string first :/. I'll work on reproducing the problem to see what is going on. (if you have a sample app, that would help)
Looks like the path we are finding from cursor to Data is int -> String -> Long instead of long -> Date which seems like the problem.
3) You can specify the TypeConverter in the Database if you want it to valid for both the entity and the Dao. @TypeConverter's are scoped to where you define so defining it in the entity does not help the Dao to resolve it.
Looks like the path we are finding from cursor to Data is int -> String -> Long instead of long -> Date which seems like the problem.
3) You can specify the TypeConverter in the Database if you want it to valid for both the entity and the Dao. @TypeConverter's are scoped to where you define so defining it in the entity does not help the Dao to resolve it.
je...@gmail.com <je...@gmail.com> #3
Here is sample project that reproduces this issue....
Side note... it would be nice if the primary key id would get auto populated after call the @Insert function on a new Entity
Side note... it would be nice if the primary key id would get auto populated after call the @Insert function on a new Entity
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)