Skip to content

Error when use suspend function with room dao methods #231

Closed
@yifeidesu

Description

@yifeidesu

I'm trying to make all database query / delete functions in the Dao class as suspend functions. I enabled kotlin coroutine in gradle, and add suspend keywork for all dao functions. Then build, and get errors for wrong param type for the auto generated dao methods.

In Dao class:

@Query("select * from myevent")
suspend fun all(): List<MyEvent> // I added the suspend keyword

@Delete
suspend fun deleteEvent(event: MyEvent)
...

Then build and get this error:

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 navigate to code from the 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 delete the generated dao class and rebuild to renegerate it, still get these errors. How can I fix this?

Activity

jcornaz

jcornaz commented on Feb 8, 2018

@jcornaz
Contributor

Adding the suspend keywork change the JVM signature of the function by adding another parameter.

this kotlin signature: suspend fun all(): List<MyEvent>
will be compiled to this java signature: public void all(cont: Continuation<List<MyEvent>>)

yifeidesu

yifeidesu commented on Feb 8, 2018

@yifeidesu
Author

Does it mean that I can not use suspend for the dao methods, because it will change signature?

yifeidesu

yifeidesu commented on Feb 9, 2018

@yifeidesu
Author

I see. I will call it in coroutine functions instead. Many thanks!

jcornaz

jcornaz commented on Feb 9, 2018

@jcornaz
Contributor

Does it mean that I can not use suspend for the dao methods, because it will change signature?

As far as I now, yes. At least if you use a library to generate the dao classes. But you can still provider helper on it.

interface EventStore {

  @Query("select * from myevent")
  fun all(): List<MyEvent>
}

// run async and await the result
suspend fun EventStore.awaitAll(): List<MyEvent> = async { all() }.await()

// ensure to run in a given context
suspend fun EventStore.awaitAll(): List<MyEvent> = withContext(CommonPool) { all() }

This are just a examples, it depends on what you really want to achieve.

jcornaz

jcornaz commented on Feb 9, 2018

@jcornaz
Contributor

And of course depending on library you use (especially if it is a Kotlin library), You may ask them to support suspending functions. Or even, improve the library yourself.

What is the library you use, if I may ask ? Spring ?

If it's spring, it looks like they decided to support Kotlin (https://spring.io/blog/2017/01/04/introducing-kotlin-support-in-spring-framework-5-0), they may support this use case, in the future.

yifeidesu

yifeidesu commented on Feb 9, 2018

@yifeidesu
Author

@jcornaz I'm using Room Persistence library for Android. So now I'm using suspend not directly in the dao class but on its wrapper methods. Also I'll try use it on the extension methods to avoid use it directly with @Query, just as the example you provided. I think for improvement, the ide can give a red squiggly line when user try to use suspend together with Room's @Query annotation. Thank you for the examples!

Ikayh19

Ikayh19 commented on Dec 22, 2021

@Ikayh19

@jcornaz I'm using Room Persistence library for Android. So now I'm using suspend not directly in the dao class but on its wrapper methods. Also I'll try use it on the extension methods to avoid use it directly with @Query, just as the example you provided. I think for improvement, the ide can give a red squiggly line when user try to use suspend together with Room's @Query annotation. Thank you for the examples!

where you able to resolve this issue? I have the same problems and can't figure out a solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jcornaz@yifeidesu@Ikayh19

        Issue actions

          Error when use suspend function with room dao methods · Issue #231 · Kotlin/kotlinx.coroutines