我的 TeamDao 里有这个
@Query("select * from teams where id = :teamId")
Flowable<Team> getRivals(int teamId);
我这样订阅
teamRepository.getRivals(61).
distinctUntilChanged().
observeOn(SchedulerProvider.getInstance().ui()).
subscribeOn(SchedulerProvider.getInstance().computation())
.subscribe(team -> Log.d("HomeActivity", team.getName()));
只要 Team 表的任何行发生任何变化,无论它是否具有 id 61,我都会看到我的订阅者在调用。
我在博客上看到 distinctUntilChanged() 正是用来避免这种情况的。
我的团队POJO是这样的
@Entity(tableName = "teams",
foreignKeys = {
@ForeignKey
(entity = User.class,
parentColumns = "id",
childColumns = "userId",
onDelete = ForeignKey.CASCADE),
@ForeignKey
(entity = Neighbourhood.class,
parentColumns = "id",
childColumns = "neighbourhoodId"
)})
public class Team {
@PrimaryKey
int id;
private String name;
private String imageUrl;
@Embedded(prefix = "ladder_")
Ladder ladder;
// USE RELATIONS WITH @Relation()
//relations
private int userId;
private int neighbourhoodId;
private int skillLevelId;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public Neighbourhood getNeighbourhood() {
return neighbourhood;
}
public void setNeighbourhood(Neighbourhood neighbourhood) {
this.neighbourhood = neighbourhood;
}
public List<TeamMember> getTeamMemberList() {
return teamMemberList;
}
public void setTeamMemberList(List<TeamMember> teamMemberList) {
this.teamMemberList = teamMemberList;
}
public int getNeighbourhoodId() {
return neighbourhoodId;
}
public void setNeighbourhoodId(int neighbourhoodId) {
this.neighbourhoodId = neighbourhoodId;
}
public int getSkillLevelId() {
return skillLevelId;
}
public void setSkillLevelId(int skillLevelId) {
this.skillLevelId = skillLevelId;
}
public List<Sport> getSports() {
return sports;
}
public void setSports(List<Sport> sportList) {
this.sports = sportList;
}
public Ladder getLadder() {
return ladder;
}
public void setLadder(Ladder ladder) {
this.ladder = ladder;
}
public List<MatchRequest> getMatchRequests() {
return matchRequests;
}
public void setMatchRequests(List<MatchRequest> matchRequests) {
this.matchRequests = matchRequests;
}
}
有什么建议吗?
最佳答案
在使用 distinct
或 distinctUntilChanged()
时,您需要在流式传输类型中实现适当的 Object.equals
作为这些运算符使用 equals
来比较项目(因为这是 Java 集合中的惯用方式)。
关于android - Room flowable调用多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48585491/