使用 Room @Insert 方法返回 Single<Long> 时出错
Error when returning Single<Long> using Room @Insert method
我正在尝试使用 RxJava 和 return 一个 Single<Long>
当插入到我的房间数据库成功时,但我在编译时出现以下错误:
Not sure how to handle insert method's return type.
Single<Long> insert(List<T> obj);
这个接口由我所有的道扩展类:
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);
}
更新
问题是,当我只有 1 个插入方法时,它可以工作,但是当我有 2 个或更多(如您所见,例如插入一个 List<T>
)时,它不起作用。 .
还有我的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'
}
似乎在 2.3.0-alpha01
中不支持专门使用 RxJava3 类型,但在 2.3.0-alpha02
中添加了支持。
特别是 2.3.0-alpha02
的 release notes 说:
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)
我正在尝试使用 RxJava 和 return 一个 Single<Long>
当插入到我的房间数据库成功时,但我在编译时出现以下错误:
Not sure how to handle insert method's return type.
Single<Long> insert(List<T> obj);
这个接口由我所有的道扩展类:
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);
}
更新
问题是,当我只有 1 个插入方法时,它可以工作,但是当我有 2 个或更多(如您所见,例如插入一个 List<T>
)时,它不起作用。 .
还有我的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'
}
似乎在 2.3.0-alpha01
中不支持专门使用 RxJava3 类型,但在 2.3.0-alpha02
中添加了支持。
特别是 2.3.0-alpha02
的 release notes 说:
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)