Room Rxjava Single 作为删除响应
Room Rxjava Single as Delete Response
我遇到了一些来自 Room Library 的编译时错误问题。
我使用的版本:2.1.0-alpha02
以下 Dao 导致错误:
@Dao()
public interface WorkoutExerciseDao {
[......]
@Update()
Single<Integer> updateWorkout(final WorkoutExercise... workoutExercises);
@Delete
Single<Integer> deleteWorkouts(final WorkoutExercise... user);
@Query("DELETE FROM workout_exercise_table WHERE id IN(:exerciseIds)")
Single<Integer> deleteWorkouts(final long... exerciseIds);
}
目前,第一个 @Delete 注释方法可以正常编译并按预期工作。如果我添加第二个(在查询方法中删除)它会中断编译并出现错误:
Deletion methods must either return void or return int (the number of
deleted rows).
我是不是漏掉了什么?
你是对的,我遇到了这个问题。我不知道原因,但我知道在最新版本的 room 中没有任何方法可以处理这个问题,当你使用查询 DELETE
它说 return type must be void or int 但是如果你想对 DELETE
查询使用 RX
你可以这样做,但可能不是最好的方法:
首先将 interface
转换为 abstract class
并将所有方法转换为 abstract method
然后
@Dao
public abstract class WorkoutExerciseDao {
@Update()
abstract Single<Integer> updateWorkout(final WorkoutExercise... workoutExercises);
@Delete
abstract Single<Integer> deleteWorkouts(final WorkoutExercise... user);
@Query("DELETE FROM workout_exercise_table WHERE id IN(:exerciseIds)")
abstract Integer deleteWorkouts(final long... exerciseIds);
Single<Integer> deleteWorkoutsById(final long... exerciseIds) {
return Single.create(emitter -> {
emitter.onSuccess(deleteWorkouts(exerciseIds));
});
}
}
您需要使用AndroidX
将您的项目迁移到 AndroidX 首先参考 documentation,简单的方法是右键单击您的项目文件夹,确保它位于 projects perspective,右击文件夹,点击Refactor,应该会有一个选项“Migrate to AndroidX”。
将项目迁移到 AndroidX 后,您现在可以在应用级别添加 AndroidX 依赖项 gradle。
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" // kapt for Kotlin
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
请参阅 documentation 以添加 Room 的最新版本依赖项。
@Query("DELETE FROM yourDB")
void delete() : Completable
我遇到了一些来自 Room Library 的编译时错误问题。
我使用的版本:2.1.0-alpha02
以下 Dao 导致错误:
@Dao()
public interface WorkoutExerciseDao {
[......]
@Update()
Single<Integer> updateWorkout(final WorkoutExercise... workoutExercises);
@Delete
Single<Integer> deleteWorkouts(final WorkoutExercise... user);
@Query("DELETE FROM workout_exercise_table WHERE id IN(:exerciseIds)")
Single<Integer> deleteWorkouts(final long... exerciseIds);
}
目前,第一个 @Delete 注释方法可以正常编译并按预期工作。如果我添加第二个(在查询方法中删除)它会中断编译并出现错误:
Deletion methods must either return void or return int (the number of deleted rows).
我是不是漏掉了什么?
你是对的,我遇到了这个问题。我不知道原因,但我知道在最新版本的 room 中没有任何方法可以处理这个问题,当你使用查询 DELETE
它说 return type must be void or int 但是如果你想对 DELETE
查询使用 RX
你可以这样做,但可能不是最好的方法:
首先将 interface
转换为 abstract class
并将所有方法转换为 abstract method
然后
@Dao
public abstract class WorkoutExerciseDao {
@Update()
abstract Single<Integer> updateWorkout(final WorkoutExercise... workoutExercises);
@Delete
abstract Single<Integer> deleteWorkouts(final WorkoutExercise... user);
@Query("DELETE FROM workout_exercise_table WHERE id IN(:exerciseIds)")
abstract Integer deleteWorkouts(final long... exerciseIds);
Single<Integer> deleteWorkoutsById(final long... exerciseIds) {
return Single.create(emitter -> {
emitter.onSuccess(deleteWorkouts(exerciseIds));
});
}
}
您需要使用AndroidX
将您的项目迁移到 AndroidX 首先参考 documentation,简单的方法是右键单击您的项目文件夹,确保它位于 projects perspective,右击文件夹,点击Refactor,应该会有一个选项“Migrate to AndroidX”。
将项目迁移到 AndroidX 后,您现在可以在应用级别添加 AndroidX 依赖项 gradle。
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" // kapt for Kotlin
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
请参阅 documentation 以添加 Room 的最新版本依赖项。
@Query("DELETE FROM yourDB")
void delete() : Completable