合并多个 LiveData 源?
Merging multiple LiveData sources?
为了更容易形象化我的问题,我画了以下内容:
我正在使用 RoomDatabase、Repository、Viewmodel 和 Livedata。区域与网关具有 1 对 n 的关系,网关与项目具有 1 对 n 的关系。
我创建了 AreaWithGateways 实体和 GatewayWithItems 实体。
物品可以从一个网关移动到另一个网关,这就是为什么我用 Livedata 观察它们以跟踪它们在哪个网关中。我现在的问题是我发现自己也需要跟踪哪些物品在哪些区域,我不知道该怎么做。
我想过将每个网关的 LiveData 合并在一起并使用 MediatorLiveData 进行观察,但我不太了解如何使用它。
或者也许可以创建实体 AreaWithGatewayswithItems?
如有任何见解,我们将不胜感激
编辑:我正在添加一些代码以使问题更清楚一些
这是区域实体
@Entity(tableName = "area_table")
public class Area {
@PrimaryKey(autoGenerate = false)
private int areaId;
private String areaName;
private float wertX;
private float wertY;
private Boolean isDrawn;
public int getAreaId() {
return areaId;
}
public void setAreaId(int areaId) {
this.areaId = areaId;
}
public String getAreaName() {
return areaName;
}
public float getWertX() {
return wertX;
}
public float getWertY() {
return wertY;
}
public Boolean getDrawn() {
return isDrawn;
}
public Area(int areaId, String areaName, float wertX, float wertY, Boolean isDrawn) {
this.areaId = areaId;
this.areaName = areaName;
this.wertX = wertX;
this.wertY = wertY;
this.isDrawn = isDrawn;
}
}
网关实体:
@Entity(tableName = "gateway_table")
public class Gateway {
private float temp;
private String title;
private int areaId;
@PrimaryKey(autoGenerate = false)
private int gatewayId;
public int getGatewayId() {
return gatewayId;
}
public void setGatewayId(int gatewayId) {
this.gatewayId = gatewayId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public float getTemp() {
return temp;
}
public void setTemp(float temp) {
this.temp = temp;
}
public int getAreaId() {
return areaId;
}
public Gateway(int areaId, int gatewayId, String title) {
this.title = title;
this.areaId = areaId;
this.gatewayId = gatewayId;
}
}
和“项目”实体:
@Entity(tableName = "cow_table")
public class Cow {
private int age;
private String sex;
private String name;
private boolean drawn;
private int gatewayId;
private String raceId;
@PrimaryKey(autoGenerate = false)
private int cowId;
public int getCowId() {
return cowId;
}
public void setCowId(int cowId) {
this.cowId = cowId;
}
public String getName() {
return name;
}
public boolean isDrawn() {
return drawn;
}
public int getAge() {
return age;
}
public String getSex() {
return sex;
}
public void setDrawn(boolean drawn) {
this.drawn = drawn;
}
public int getGatewayId() {
return gatewayId;
}
public String getRaceId() {
return raceId;
}
public Cow(int age, int cowId, int gatewayId,String name, String raceId, String sex, boolean drawn) {
this.age = age;
this.cowId = cowId;
this.sex= sex;
this.name = name;
this.drawn = drawn;
this.gatewayId = gatewayId;
this.raceId = raceId;
}
}
这是关系“AreaWithGateways”:
public class AreaWithGateways {
@Embedded
private Area area;
@Relation(parentColumn = "areaId",
entityColumn = "areaId")
private List<Gateway> gatewayList;
public Area getArea() {
return area;
}
public List<Gateway> getGatewayList() {
return gatewayList;
}
public AreaWithGateways(Area area, List<Gateway> gatewayList) {
this.area = area;
this.gatewayList = gatewayList;
}
}
以及 GatewaysWithCows:
public class GatewayWithCows {
@Embedded
private Gateway gateway;
@Relation(parentColumn = "gatewayId",
entityColumn = "gatewayId")
private List<Cow> cowList;
public Gateway getGateway() {
return gateway;
}
public List<Cow> getCowList() {
return cowList;
}
public GatewayWithCows(Gateway gateway, List<Cow> cowList) {
this.gateway = gateway;
this.cowList = cowList;
}
}
我一直在尝试找到一种方法来将一个区域中的所有“项目”作为实时数据获取,但仍然无法弄清楚。
我觉得我应该以某种方式使用 AreaWithGateways 将 LiveData 项目添加在一起,但我无法通过网关访问这些项目,它必须是相反的方式。
Or maybe it's possible to create an Entity AreaWithGatewayswithItems?
不是实体,因为它们用于定义 table,但是通过使用 @Embedded 和 @Relation 注释的 POJO(例如,您的 GatewayWithCows 是一个 POJO)。
I feel like I should somehow use AreaWithGateways to add the LiveData items together but I can't reach the items through the gateways, it has to be the other way around.
您基本上使用分层方法,但 POJO 是这样的,因为您有 GatewayWithCows
然后按照 :-
从 Area 与此相关
class AreaWithGatewayWithCows {
@Embedded
Area area;
@Relation(entity = Gateway.class, parentColumn = "areaId",
entityColumn = "areaId")
List<GatewayWithCows> gatewayWithCowsList;
}
注意 我错过了 s 网关后的 class 名称(因此在下面的查询)
注意 需要使用 entity = Gateway.class
因为关系是通过网关而不是通过GatewayWithCows(不是 table)。
Query Dao 可以像这样简单:-
@Query("SELECT * FROM area_table")
List<AreaWithGatewayWithCows> getAreaWithGatewaysWithCows();
- 针对 LiveData 进行了相应修改。
- 请注意,如果您在查询中使用 JOINS,则 WHERE 等任何子句只会影响 Area 的,而不影响底层的 Gateways 和 Cows。这与查询无关 @Relation 构建每个
Area
与 ALL Gateway
s 相关 Area
;每个 Gateway
得到 ALL 与 Gateway
. 相关的 Cow
为了更容易形象化我的问题,我画了以下内容:
我正在使用 RoomDatabase、Repository、Viewmodel 和 Livedata。区域与网关具有 1 对 n 的关系,网关与项目具有 1 对 n 的关系。 我创建了 AreaWithGateways 实体和 GatewayWithItems 实体。
物品可以从一个网关移动到另一个网关,这就是为什么我用 Livedata 观察它们以跟踪它们在哪个网关中。我现在的问题是我发现自己也需要跟踪哪些物品在哪些区域,我不知道该怎么做。
我想过将每个网关的 LiveData 合并在一起并使用 MediatorLiveData 进行观察,但我不太了解如何使用它。 或者也许可以创建实体 AreaWithGatewayswithItems?
如有任何见解,我们将不胜感激
编辑:我正在添加一些代码以使问题更清楚一些
这是区域实体
@Entity(tableName = "area_table")
public class Area {
@PrimaryKey(autoGenerate = false)
private int areaId;
private String areaName;
private float wertX;
private float wertY;
private Boolean isDrawn;
public int getAreaId() {
return areaId;
}
public void setAreaId(int areaId) {
this.areaId = areaId;
}
public String getAreaName() {
return areaName;
}
public float getWertX() {
return wertX;
}
public float getWertY() {
return wertY;
}
public Boolean getDrawn() {
return isDrawn;
}
public Area(int areaId, String areaName, float wertX, float wertY, Boolean isDrawn) {
this.areaId = areaId;
this.areaName = areaName;
this.wertX = wertX;
this.wertY = wertY;
this.isDrawn = isDrawn;
}
}
网关实体:
@Entity(tableName = "gateway_table")
public class Gateway {
private float temp;
private String title;
private int areaId;
@PrimaryKey(autoGenerate = false)
private int gatewayId;
public int getGatewayId() {
return gatewayId;
}
public void setGatewayId(int gatewayId) {
this.gatewayId = gatewayId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public float getTemp() {
return temp;
}
public void setTemp(float temp) {
this.temp = temp;
}
public int getAreaId() {
return areaId;
}
public Gateway(int areaId, int gatewayId, String title) {
this.title = title;
this.areaId = areaId;
this.gatewayId = gatewayId;
}
}
和“项目”实体:
@Entity(tableName = "cow_table")
public class Cow {
private int age;
private String sex;
private String name;
private boolean drawn;
private int gatewayId;
private String raceId;
@PrimaryKey(autoGenerate = false)
private int cowId;
public int getCowId() {
return cowId;
}
public void setCowId(int cowId) {
this.cowId = cowId;
}
public String getName() {
return name;
}
public boolean isDrawn() {
return drawn;
}
public int getAge() {
return age;
}
public String getSex() {
return sex;
}
public void setDrawn(boolean drawn) {
this.drawn = drawn;
}
public int getGatewayId() {
return gatewayId;
}
public String getRaceId() {
return raceId;
}
public Cow(int age, int cowId, int gatewayId,String name, String raceId, String sex, boolean drawn) {
this.age = age;
this.cowId = cowId;
this.sex= sex;
this.name = name;
this.drawn = drawn;
this.gatewayId = gatewayId;
this.raceId = raceId;
}
}
这是关系“AreaWithGateways”:
public class AreaWithGateways {
@Embedded
private Area area;
@Relation(parentColumn = "areaId",
entityColumn = "areaId")
private List<Gateway> gatewayList;
public Area getArea() {
return area;
}
public List<Gateway> getGatewayList() {
return gatewayList;
}
public AreaWithGateways(Area area, List<Gateway> gatewayList) {
this.area = area;
this.gatewayList = gatewayList;
}
}
以及 GatewaysWithCows:
public class GatewayWithCows {
@Embedded
private Gateway gateway;
@Relation(parentColumn = "gatewayId",
entityColumn = "gatewayId")
private List<Cow> cowList;
public Gateway getGateway() {
return gateway;
}
public List<Cow> getCowList() {
return cowList;
}
public GatewayWithCows(Gateway gateway, List<Cow> cowList) {
this.gateway = gateway;
this.cowList = cowList;
}
}
我一直在尝试找到一种方法来将一个区域中的所有“项目”作为实时数据获取,但仍然无法弄清楚。 我觉得我应该以某种方式使用 AreaWithGateways 将 LiveData 项目添加在一起,但我无法通过网关访问这些项目,它必须是相反的方式。
Or maybe it's possible to create an Entity AreaWithGatewayswithItems?
不是实体,因为它们用于定义 table,但是通过使用 @Embedded 和 @Relation 注释的 POJO(例如,您的 GatewayWithCows 是一个 POJO)。
I feel like I should somehow use AreaWithGateways to add the LiveData items together but I can't reach the items through the gateways, it has to be the other way around.
您基本上使用分层方法,但 POJO 是这样的,因为您有 GatewayWithCows
然后按照 :-
class AreaWithGatewayWithCows {
@Embedded
Area area;
@Relation(entity = Gateway.class, parentColumn = "areaId",
entityColumn = "areaId")
List<GatewayWithCows> gatewayWithCowsList;
}
注意 我错过了 s 网关后的 class 名称(因此在下面的查询)
注意 需要使用
entity = Gateway.class
因为关系是通过网关而不是通过GatewayWithCows(不是 table)。
Query Dao 可以像这样简单:-
@Query("SELECT * FROM area_table")
List<AreaWithGatewayWithCows> getAreaWithGatewaysWithCows();
- 针对 LiveData 进行了相应修改。
- 请注意,如果您在查询中使用 JOINS,则 WHERE 等任何子句只会影响 Area 的,而不影响底层的 Gateways 和 Cows。这与查询无关 @Relation 构建每个
Area
与 ALLGateway
s 相关Area
;每个Gateway
得到 ALL 与Gateway
. 相关的
Cow