ole_thoeb
ole_thoeb

Reputation: 63

Using kotlin coroutines with room

In 2.1 Room added support for coroutines, but I can't get it working. It should be as easy as adding the dependency but somehow I'm overlooking something.

In my build.gradle I got the dependencies for coroutines, room and room-coroutines

dependencies { 
    def room_version = "2.2.0-beta01"
    // Room components
    implementation "android.arch.persistence.room:runtime:$room_version"
    kapt "android.arch.persistence.room:compiler:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    androidTestImplementation "android.arch.persistence.room:testing:$room_version"
    def coroutines_version = "1.1.1"
    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
}

I allready tryed resyncing gradle, cleaning and rebuilding the project.

In my Doa i have methods like the following

@Dao
interface PlanDao {
    @Insert
    suspend fun insertVerPlan(verPlan: SqlVerPlan)
}

When trying to build the project Room doesn't know how to handle the suspending functions and the following error occurs:

error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
    kotlin.coroutines.Continuation<? super kotlin.Unit> p1);
                                                        ^

error: Methods annotated with @Insert can return either void, long, Long, long[], Long[] or List<Long>.
    public abstract java.lang.Object insertVerPlan(@org.jetbrains.annotations.NotNull()
                                     ^

I seriously don't know what I'm missing and I can't find anyone with the same problem since the new Room version.

Upvotes: 4

Views: 4483

Answers (1)

Kiskae
Kiskae

Reputation: 25573

You're mixing different versions of the room library.

android.arch.persistence.room:runtime should be androidx.room:room-runtime

android.arch.persistence.room:compiler should be androidx.room:room-compiler

android.arch.persistence.room:testing should be androidx.room:room-testing

as per Room#Declaring dependencies

Since you're using the old coordinates for the compiler it does not know about the suspend support.

Upvotes: 4

Related Questions