mantc_sdr
mantc_sdr

Reputation: 491

Error when returning Single<Long> using Room @Insert method

I am trying to use RxJava and return a Single<Long> when an insert to my room database is successful, but I have the following error when compiling:

Not sure how to handle insert method's return type.
    Single<Long> insert(List<T> obj);

This interface is extended by all my Dao classes:

import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import io.reactivex.rxjava3.core.Single;
    
public interface MyAppDao<T> {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    Single<Long> insertOrReplace(T obj);

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    Single<Long> insert(List<T> obj);
}

UPDATE

The trouble is that when I just have 1 insert method, it works, but when I have 2 or more (as you can see, for example to insert a List<T>) it does not work...

And my build.gradle:

plugins {
    id 'com.android.application'
}

apply plugin: 'dagger.hilt.android.plugin'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"
defaultConfig {
    applicationId "com.gimlite"
    minSdkVersion 21
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

buildFeatures {
    dataBinding true
}

}

dependencies {
def room_version = "2.3.0-alpha01"
def work_manager_version = "2.3.4"
def nav_version = "2.1.0"
def rxjava_version = "3.0.7"
def rxandroid_version = "3.0.0";
def retrofit_version = "2.9.0";
def lifecycle_version = "2.2.0"
def timber_version = "4.7.1";

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.annotation:annotation:1.1.0'

//UI
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

//ViewModel && LiveData
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0'
implementation "android.arch.lifecycle:extensions:1.1.0"
implementation "android.arch.lifecycle:viewmodel:$lifecycle_version"
implementation "android.arch.lifecycle:reactivestreams:$lifecycle_version"

//Room
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-rxjava3:$room_version"

//Dagger Core
implementation "com.google.dagger:dagger:2.28"
annotationProcessor "com.google.dagger:dagger-compiler:2.28"

//Dagger Android
api 'com.google.dagger:dagger-android:2.27'
api 'com.google.dagger:dagger-android-support:2.27'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.27'

//Hilt
implementation "com.google.dagger:hilt-android:2.28-alpha"
annotationProcessor "com.google.dagger:hilt-android-compiler:2.28-alpha"

//Retrofit
implementation "com.squareup.retrofit2:adapter-rxjava3:$retrofit_version"
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"

//OkHttp
implementation 'com.squareup.okhttp3:okhttp:4.2.2'
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.2'

//ReactiveX
implementation "io.reactivex.rxjava3:rxjava:$rxjava_version"
implementation "io.reactivex.rxjava3:rxandroid:$rxandroid_version"

//RecyclerView
implementation 'androidx.recyclerview:recyclerview:1.0.0'

//PlayServices
implementation 'com.google.android.gms:play-services-location:17.0.0'

//Fragment Navigation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"

//Soap
implementation files('libs/ksoap2-android-assembly-3.6.4-jar-with-dependencies.jar') {
    configurations {
        compile.exclude module: 'okhttp'
    }
}

//Timber (Logging)
implementation "com.jakewharton.timber:timber:$timber_version"

//Stetho
implementation 'com.facebook.stetho:stetho:1.5.1'

//TESTING
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

Upvotes: 0

Views: 519

Answers (1)

Henry Twist
Henry Twist

Reputation: 5980

It seems like using RxJava3 types specifically are not supported in 2.3.0-alpha01 but support has been added in 2.3.0-alpha02.

Specifically the release notes for 2.3.0-alpha02 say:

RxJava3 Support: Room now supports RxJava3 types. Similar to RxJava2 you can declare DAO methods whose return type are Flowable, Single, Maybe and Completable. Additionally a new artifact androidx.room:room-rxjava3 is available to support RxJava3. (b/152427884)

Upvotes: 1

Related Questions