If you are trying to use suspend
functions in your DAO with Room, you will probably face this compilation error:
error: Not sure how to handle query method's return type (java.lang.Object). DELETE query methods must either return void or int (the number of deleted rows).
In my case, Dao looks like this:
@Dao internal interface BooksDao { @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun insertBook(book: BookDbModel): Long @Query("DELETE FROM ${BookDbModel.TABLE_NAME} WHERE ${BookDbModel.ID} = :bookId") suspend fun deleteBook(bookId: Long)
It happens because Kotlin adds additional arguments to the function, for example:
@androidx.room.Query(value = "DELETE FROM books WHERE id = :bookId")
@org.jetbrains.annotations.Nullable()
public abstract java.lang.Object deleteBook(long bookId, @org.jetbrains.annotations.NotNull()
kotlin.coroutines.Continuation<? super kotlin.Unit> $completion);
To fix it, you need to add annotationProcessor
in your build.gradle
.
Original code looks like this:
implementation(Dependencies.Libraries.ROOM_RUNTIME)
implementation(Dependencies.Libraries.ROOM_KTX)
implementation(Dependencies.Libraries.ROOM_PAGING)
kapt(Dependencies.Libraries.ROOM_COMPILER)
annotationProcessor(Dependencies.Libraries.ROOM_COMPILER)
You need to replace it with this one:
implementation(Dependencies.Libraries.ROOM_RUNTIME)
implementation(Dependencies.Libraries.ROOM_KTX)
implementation(Dependencies.Libraries.ROOM_PAGING)
annotationProcessor(Dependencies.Libraries.ROOM_COMPILER)
Re-build your project, and now everything works!
Leave a Reply