使用 RxJava 2 合并 Android 个房间查询的结果
Merging results of Android Room queries with RxJava 2
对于我正在使用 Android Room 作为本地持久层和 RxJava2 构建的应用程序,我 运行 遇到了一个我无法解决的问题。
请记住,我是 RxJava 的新手。
所以我在 Room 数据库中有 2 个(或更多)实体。例如:
@Entity(tableName = "physical_tests", indices = {@Index(value = {"uuid", "day", "user_id"}, unique = true)})
public class PhysicalTest extends Task {
@ColumnInfo(name = "exercise_duration")
private long exerciseDuration;
public PhysicalTest() {
super(Type.physic, R.string.fa_icon_physical_test);
}
public long getExerciseDuration() {
return exerciseDuration;
}
public void setExerciseDuration(long exerciseDuration) {
this.exerciseDuration = exerciseDuration;
}
}
并且:
@Entity(tableName = "plate_controls", indices = {@Index(value = {"uuid", "day", "user_id"}, unique = true)})
public class PlateControl extends Task {
@ColumnInfo(name = "plate_switched_on")
private boolean mPlateSwitchedOn;
public PlateControl() {
super(Type.plate, R.string.fa_icon_plate_control);
}
public boolean isPlateSwitchedOn() {
return mPlateSwitchedOn;
}
public void setPlateSwitchedOn(boolean mPlateSwitchedOn) {
this.mPlateSwitchedOn = mPlateSwitchedOn;
}
}
如您所见,两者都有一个 Task 超类。现在,如果我想创建一个查询来获取所有任务的列表(PhysicalTests+PlateControls),我将如何使用 RxJava 做到这一点?
现在我有 2 个查询 return 可能在我的 Dao 中:
@Query("SELECT * FROM plate_controls")
Maybe<List<PlateControl>> getAllPlateControls();
@Query("SELECT * FROM physical_tests")
Maybe<List<PhysicalTest>> getAllPhysicalTests();
简单地合并这些似乎不起作用,因为 return 类型不符合 List<Task>
:
public Maybe<List<Task>> getAllTasks() {
return Maybe.merge(mTaskDao.getAllPhysicalTests(), mTaskDao.getAllPlateControls());
}
(如果这看起来有点矫枉过正,我实际上有几个 Task 的子类要合并)
您可以直接 zip
最多 9 个来源(如果通过 Iterable
来源提供,则可以是任意数量的来源):
public Maybe<List<Task>> getAllTasks() {
return Maybe.zip(
mTaskDao.getAllPhysicalTests(),
mTaskDao.getAllPlateControls(),
mTaskDao.getAllX(),
mTaskDao.getAllY(),
mTaskDao.getAllZ(),
(physicalTests, plateControls, xs, ys, zs) -> {
List<Task> combined = new ArrayList<>();
combined.addAll(physicalTests);
combined.addAll(plateControls);
combined.addAll(xs);
combined.addAll(ys);
combined.addAll(zs);
return combined;
}
);
}
对于我正在使用 Android Room 作为本地持久层和 RxJava2 构建的应用程序,我 运行 遇到了一个我无法解决的问题。 请记住,我是 RxJava 的新手。
所以我在 Room 数据库中有 2 个(或更多)实体。例如:
@Entity(tableName = "physical_tests", indices = {@Index(value = {"uuid", "day", "user_id"}, unique = true)})
public class PhysicalTest extends Task {
@ColumnInfo(name = "exercise_duration")
private long exerciseDuration;
public PhysicalTest() {
super(Type.physic, R.string.fa_icon_physical_test);
}
public long getExerciseDuration() {
return exerciseDuration;
}
public void setExerciseDuration(long exerciseDuration) {
this.exerciseDuration = exerciseDuration;
}
}
并且:
@Entity(tableName = "plate_controls", indices = {@Index(value = {"uuid", "day", "user_id"}, unique = true)})
public class PlateControl extends Task {
@ColumnInfo(name = "plate_switched_on")
private boolean mPlateSwitchedOn;
public PlateControl() {
super(Type.plate, R.string.fa_icon_plate_control);
}
public boolean isPlateSwitchedOn() {
return mPlateSwitchedOn;
}
public void setPlateSwitchedOn(boolean mPlateSwitchedOn) {
this.mPlateSwitchedOn = mPlateSwitchedOn;
}
}
如您所见,两者都有一个 Task 超类。现在,如果我想创建一个查询来获取所有任务的列表(PhysicalTests+PlateControls),我将如何使用 RxJava 做到这一点?
现在我有 2 个查询 return 可能在我的 Dao 中:
@Query("SELECT * FROM plate_controls")
Maybe<List<PlateControl>> getAllPlateControls();
@Query("SELECT * FROM physical_tests")
Maybe<List<PhysicalTest>> getAllPhysicalTests();
简单地合并这些似乎不起作用,因为 return 类型不符合 List<Task>
:
public Maybe<List<Task>> getAllTasks() {
return Maybe.merge(mTaskDao.getAllPhysicalTests(), mTaskDao.getAllPlateControls());
}
(如果这看起来有点矫枉过正,我实际上有几个 Task 的子类要合并)
您可以直接 zip
最多 9 个来源(如果通过 Iterable
来源提供,则可以是任意数量的来源):
public Maybe<List<Task>> getAllTasks() {
return Maybe.zip(
mTaskDao.getAllPhysicalTests(),
mTaskDao.getAllPlateControls(),
mTaskDao.getAllX(),
mTaskDao.getAllY(),
mTaskDao.getAllZ(),
(physicalTests, plateControls, xs, ys, zs) -> {
List<Task> combined = new ArrayList<>();
combined.addAll(physicalTests);
combined.addAll(plateControls);
combined.addAll(xs);
combined.addAll(ys);
combined.addAll(zs);
return combined;
}
);
}