无法在 Room dao 接口中使用 Completable
Unable to use Completable in Room dao interface
我正在尝试在 @Insert
注释方法上使用 Dao class 中的 Completable
,但是当尝试编译项目时,我遇到了这个错误:
error: Methods annotated with @Insert can return either void, long, Long, long[], Long[] or List<Long>.
public abstract io.reactivex.Completable insert(@org.jetbrains.annotations.NotNull()
这里是我的相关代码:
@Insert
fun insert(kanal: Kanal): Completable
@Update
fun update(kanal: Kanal): Completable
@Delete
fun delete(kanal: Kanal): Completable
我的依赖项:
def room_version = "1.1.1"
implementation "android.arch.persistence.room:runtime:$room_version"
kapt "android.arch.persistence.room:compiler:$room_version"
implementation "android.arch.persistence.room:rxjava2:$room_version"
implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
据此link Room 支持它。
@Insert, @Update, and @Delete methods: Room 2.1.0 and higher supports
return values of type Completable, Single, and Maybe.
将您的房间从 1.1.1
更新到 2.1.0
或更高,它会起作用。
Completable 不适用于 1.1.1 版 Room 中的 @Insert
。您必须使用 2.1.0 或更高版本,该版本仅在 Android X 中可用。
确保使用这些依赖项而不是常规 android.arch.persistence.room
:
def room_version = "2.2.0-alpha02"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version" // For Java use annotationProcessor instead of kapt
implementation "androidx.room:room-rxjava2:$room_version"
testImplementation "androidx.room:room-testing:$room_version"
这是提供 Room 的所有 Android X 依赖项的 link。
如果你不想更新 Room 的版本,你可以尝试像这样制作一个 Completable return:
fun insertSomthing():Completable{
return Completable.fromAction{
insert(kanal: Kanal)
}.subscribeOn(Schedulers.io())
}
以上对我不起作用,这是我用来修复它的方法
def room_version = "2.2.6"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// optional - Test helpers
testImplementation "androidx.room:room-testing:$room_version"
我正在尝试在 @Insert
注释方法上使用 Dao class 中的 Completable
,但是当尝试编译项目时,我遇到了这个错误:
error: Methods annotated with @Insert can return either void, long, Long, long[], Long[] or List<Long>.
public abstract io.reactivex.Completable insert(@org.jetbrains.annotations.NotNull()
这里是我的相关代码:
@Insert
fun insert(kanal: Kanal): Completable
@Update
fun update(kanal: Kanal): Completable
@Delete
fun delete(kanal: Kanal): Completable
我的依赖项:
def room_version = "1.1.1"
implementation "android.arch.persistence.room:runtime:$room_version"
kapt "android.arch.persistence.room:compiler:$room_version"
implementation "android.arch.persistence.room:rxjava2:$room_version"
implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
据此link Room 支持它。
@Insert, @Update, and @Delete methods: Room 2.1.0 and higher supports return values of type Completable, Single, and Maybe.
将您的房间从 1.1.1
更新到 2.1.0
或更高,它会起作用。
Completable 不适用于 1.1.1 版 Room 中的 @Insert
。您必须使用 2.1.0 或更高版本,该版本仅在 Android X 中可用。
确保使用这些依赖项而不是常规 android.arch.persistence.room
:
def room_version = "2.2.0-alpha02"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version" // For Java use annotationProcessor instead of kapt
implementation "androidx.room:room-rxjava2:$room_version"
testImplementation "androidx.room:room-testing:$room_version"
这是提供 Room 的所有 Android X 依赖项的 link。
如果你不想更新 Room 的版本,你可以尝试像这样制作一个 Completable return:
fun insertSomthing():Completable{
return Completable.fromAction{
insert(kanal: Kanal)
}.subscribeOn(Schedulers.io())
}
以上对我不起作用,这是我用来修复它的方法
def room_version = "2.2.6"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// optional - Test helpers
testImplementation "androidx.room:room-testing:$room_version"