question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when use suspend function with room dao methods

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Ikayh19commented, Dec 22, 2021

@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

2reactions
yifeidesucommented, Feb 9, 2018

@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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error with Room dao class when using Kotlin coroutines
In that case, if the suspend method isn't used in Dao, this activity will crash. It means that it is impossible to get...
Read more >
When using suspend fun in Room Dao methods, following ...
When using suspend fun in Room Dao methods, following error: Not sure how to convert a Cursor to this method's return type (java.lang.Object)....
Read more >
Kapt: "error: Type of the parameter must be a class annotated ...
This app uses the keyword "suspend" in the Dao (using Room). Feels like that keyword is crashing the program. I'll show the code...
Read more >
Room Coroutines. Add some suspense to your database
DAO methods can now be marked as suspending to ensure that they are not executed on the main thread. Read on to see...
Read more >
Kotlin flows on Android - Android Developers
For example, you can use a flow to receive live updates from a database ... As a suspend function cannot return multiple consecutive...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found