返回 Single<Long> 时出错使用 Room @Insert 方法

编程入门 行业动态 更新时间:2024-10-23 06:20:39
本文介绍了返回 Single<Long> 时出错使用 Room @Insert 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 RxJava 并在插入到我的房间数据库成功时返回 Single,但在编译时出现以下错误:

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);

我所有的Dao类都扩展了这个接口:

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); }

更新

问题是,当我只有 1 个插入方法时,它可以工作,但是当我有 2 个或更多(如您所见,例如插入 List)时,它确实有效不工作...

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...

还有我的 build.gradle:

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' }

推荐答案

2.3.0-alpha01 似乎不支持使用 RxJava3 类型,但 2.3 中添加了支持.0-alpha02.

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.

特别是 发行说明 2.3.0-alpha02 说:

RxJava3 支持:Room 现在支持 RxJava3 类型.与 RxJava2 类似,您可以声明返回类型为 Flowable、Single、Maybe 和 Completable 的 DAO 方法.此外,一个新的工件 androidx.room:room-rxjava3 可用于支持 RxJava3.(b/152427884)

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)

更多推荐

返回 Single<Long> 时出错使用 Room @Insert 方法

本文发布于:2023-10-29 03:38:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538613.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   Long   lt   Single   Insert

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!