I am trying to use kotlin coroutines to access the database in the database using the method described below, a plugin and dependency are added and kotlin coroutines is included in gradle.
in gradle file:
kotlin { experimental { coroutines 'enable' } } dependencies { implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21" ...}
So, I added the suspend keyword for all methods in the dao class, for example:
dao class
@Query("select * from myevent") suspend fun all(): List<MyEvent> @Delete suspend fun deleteEvent(event: MyEvent) ...
and build, then get these errors
Mistake
e: C:\Users\projectpath\app\build\tmp\kapt3\stubs\debug\com\robyn\myapp\data\source\local\EventsDao.java:39: error: Deletion methods must either return void or return int (the number of deleted rows). public abstract java.lang.Object deleteEventById(@org.jetbrains.annotations.NotNull() ^ e: C:\Users\projectpath\app\build\tmp\kapt3\stubs\debug\com\robyn\myapp\data\source\local\EventsDao.java:41: error: 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. kotlin.coroutines.experimental.Continuation<? super kotlin.Unit> p1);
error links go to the automatically generated dao class. The created methods in this class now have an additional parameter of this type Continuation , since this:
auto-generated dao class
@org.jetbrains.annotations.Nullable() @android.arch.persistence.room.Delete() public abstract java.lang.Object deleteAllEvents(@org.jetbrains.annotations.NotNull() // error indicates at this line java.util.List<com.robyn.myapp.data.MyEvent> events, @org.jetbrains.annotations.NotNull() kotlin.coroutines.experimental.Continuation<? super kotlin.Unit> p1); // error indicates at this line ...
I tried deleting the generated dao class and rebuilding to renounce it, still get these errors. I believe that you do not use the lauch{} method, but use the suspend keyword, because there are many places in the code for the db request.
How can i fix this?
coroutine android kotlin android-room
Robin
source share