Fixed
Status Update
Comments
ap...@google.com <ap...@google.com> #2
Project: platform/frameworks/support
Branch: androidx-master-dev
commit b2fbf27c3376b4f36a38b03f2ea2c6a1a5f67241
Author: Daniel Santiago Rivera <danysantiago@google.com>
Date: Fri Jan 18 11:33:40 2019
Fix suspend Dao methods with TypeConverters
When declaring a DAO method 'suspend fun method(): List<Date>' that
should use a TypeConverter the resulting Java impl signature ends up
with a continuation object with type arg <? super List<? extends Date>>.
Thus failing to find a row adapter for List<? extends Date> since the
TypeConverter's return type List<Data> is not assignable. This CL fixes
such issue by extending the bounds on the List's type arg when trying
to find a row adapter for it.
Bug: 122988159
Test: ./gradlew room:integration-tests:room-testapp-kotlin:cC
Change-Id: I8271e98725640afcb47c6de6adfaf03578f271b3
M room/compiler/src/main/kotlin/androidx/room/solver/TypeAdapterStore.kt
M room/integration-tests/kotlintestapp/src/androidTest/java/androidx/room/integration/kotlintestapp/dao/BooksDao.kt
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/dao/UserDao.java
https://android-review.googlesource.com/878892
https://goto.google.com/android-sha1/b2fbf27c3376b4f36a38b03f2ea2c6a1a5f67241
Branch: androidx-master-dev
commit b2fbf27c3376b4f36a38b03f2ea2c6a1a5f67241
Author: Daniel Santiago Rivera <danysantiago@google.com>
Date: Fri Jan 18 11:33:40 2019
Fix suspend Dao methods with TypeConverters
When declaring a DAO method 'suspend fun method(): List<Date>' that
should use a TypeConverter the resulting Java impl signature ends up
with a continuation object with type arg <? super List<? extends Date>>.
Thus failing to find a row adapter for List<? extends Date> since the
TypeConverter's return type List<Data> is not assignable. This CL fixes
such issue by extending the bounds on the List's type arg when trying
to find a row adapter for it.
Bug: 122988159
Test: ./gradlew room:integration-tests:room-testapp-kotlin:cC
Change-Id: I8271e98725640afcb47c6de6adfaf03578f271b3
M room/compiler/src/main/kotlin/androidx/room/solver/TypeAdapterStore.kt
M room/integration-tests/kotlintestapp/src/androidTest/java/androidx/room/integration/kotlintestapp/dao/BooksDao.kt
M room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/dao/UserDao.java
da...@google.com <da...@google.com> #3
A fix for this issue will be available in 2.1.0-alpha04
sk...@gmail.com <sk...@gmail.com> #4
Don't fixed.
For method:
@Query("SELECT * FROM downhills WHERE track_id = :trackId AND type = :type")
suspend fun getByTrackIdAndType(trackId: Long, type: DownhillEntity.Type): List<DownhillEntity>
Where DownhillEntity.Type has TypeConverter, and has corresponding annotation in my RoomDatabase class.
Generated code for this method:
@Override
public Object getByTrackIdAndType(final long trackId, final DownhillEntity.Type type,
final Continuation<? super List<DownhillEntity>> p2) {
final String _sql = "SELECT * FROM downhills WHERE track_id = ? AND type = ?";
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 2);
int _argIndex = 1;
_statement.bindLong(_argIndex, trackId);
_argIndex = 2;
final String _tmp;
_tmp = __downhillTypeConverter.fromDownhillType(type);
if (_tmp == null) {
_statement.bindNull(_argIndex);
} else {
_statement.bindString(_argIndex, _tmp);
}
final Cursor _cursor = DBUtil.query(__db, _statement, false);
try {
final Object _result;
if(_cursor.moveToFirst()) {
_result = new Object();
} else {
_result = null;
}
return _result;
} finally {
_cursor.close();
_statement.release();
}
}
It's incorrect.
For method:
@Query("SELECT * FROM downhills WHERE track_id = :trackId AND type = :type")
suspend fun getByTrackIdAndType(trackId: Long, type: DownhillEntity.Type): List<DownhillEntity>
Where DownhillEntity.Type has TypeConverter, and has corresponding annotation in my RoomDatabase class.
Generated code for this method:
@Override
public Object getByTrackIdAndType(final long trackId, final DownhillEntity.Type type,
final Continuation<? super List<DownhillEntity>> p2) {
final String _sql = "SELECT * FROM downhills WHERE track_id = ? AND type = ?";
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 2);
int _argIndex = 1;
_statement.bindLong(_argIndex, trackId);
_argIndex = 2;
final String _tmp;
_tmp = __downhillTypeConverter.fromDownhillType(type);
if (_tmp == null) {
_statement.bindNull(_argIndex);
} else {
_statement.bindString(_argIndex, _tmp);
}
final Cursor _cursor = DBUtil.query(__db, _statement, false);
try {
final Object _result;
if(_cursor.moveToFirst()) {
_result = new Object();
} else {
_result = null;
}
return _result;
} finally {
_cursor.close();
_statement.release();
}
}
It's incorrect.
yb...@google.com <yb...@google.com>
da...@google.com <da...@google.com> #5
Данил can you share a sample project that reproduces the issue describes in comment #4 ? Is your suspend function defined in a super interface or class? It seems that Room is failing to detect getByTrackIdAndType() is a suspend function and is generating a blocking implementation, this doesn't seem to be related to the usage of TypeConverters.
sk...@gmail.com <sk...@gmail.com>
da...@google.com <da...@google.com> #6
Данил thanks for the sample project. It seems Room is failing to detect that the function is a suspend function. I've created a separate issue (https://issuetracker.google.com/issues/123767877 ) since its not related to the TypeConverters issue that you originally reported.
Description
Version used: 2.1.0-alpha03
Devices/Android versions reproduced on: compilation error
When using suspend SELECT method with TypeConverter parameter, room-compiler generate incorrect method code with return type java.lang.Object.
Compilation exceptions:
• The query returns some columns [...] which are not used by java.lang.Object. You can use @ColumnInfo annotation on the fields to specify the mapping. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH).
• Not sure how to convert a Cursor to this method's return type (java.lang.Object).
• Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.
But when I remove suspend modifier all is ok.