Muhammad Ahmed AbuTalib
Muhammad Ahmed AbuTalib

Reputation: 4302

Room flowable invokes multiple times

I have this in my TeamDao

@Query("select * from teams where id = :teamId")
    Flowable<Team> getRivals(int teamId);

I subscribe to this like this

teamRepository.getRivals(61).
                distinctUntilChanged().
                observeOn(SchedulerProvider.getInstance().ui()).

                subscribeOn(SchedulerProvider.getInstance().computation())
                .subscribe(team -> Log.d("HomeActivity", team.getName()));

Whenever there is any change in any row of the Team table regardless of whether it is with the id 61 or not , I see my this subscriber invoking.

I read at a blog that distinctUntilChanged() is exactly used to avoid this.

My Team POJO is like this

@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;
    }
}

Any pointers?

Upvotes: 3

Views: 1325

Answers (1)

akarnokd
akarnokd

Reputation: 69997

When working with distinct or distinctUntilChanged(), you need a proper Object.equals implemented in the type you are streaming as these operators use equals to compare items (as this is the idiomatic way in Java Collections).

Upvotes: 6

Related Questions