房间可流动多次调用

时间:2018-02-02 15:06:15

标签: android rx-java2 rx-android android-room

我在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;
    }
}

任何指针?

1 个答案:

答案 0 :(得分:2)

使用import csv, json, sys def find_deep_value(d, key): # Modified from https://stackoverflow.com/questions/48568649/convert-json-to-csv-using-python/48569129#48569129 if key in d: return d[key] for k in d.keys(): if isinstance(d[k], dict): for j in find_deep_value(d[k], key): return j inputFile = open("pywu.cache.json", 'r') # open json file outputFile = open("CurrentObs.csv", 'w') # load csv file data = json.load(inputFile) # load json content inputFile.close() # close the input file output = csv.writer(outputFile) # create a csv.write # Gives you latitude coordinates from within the json lat = find_deep_value(data, "latitude") # Gives you longitude coordinates from within the json lon = find_deep_value(data, "longitude") # Gives you a list of weather from within the json weather = find_deep_value(data, "weather") # Gives you a list of temperature_strings from within the json temp = find_deep_value(data, "temperature_string") output.writerow([lat, lon, weather, temp]) outputFile.close() distinct时,您需要在您正在流式传输的类型中实施distinctUntilChanged(),因为这些运营商使用Object.equals来比较项目(因为这是Java Collections中的惯用方法。)