Firebase中的重复属性

时间:2018-01-06 14:38:39

标签: java android firebase firebase-realtime-database

我有一个像这样的模型NewsFeedItem:

public class NewsFeedItem {
    @PropertyName("like_number")
    protected int likeCount = 0;

    @PropertyName("time")
    protected long timestamp;

    @PropertyName("ownerUid")
    protected String ownerUid;

    @PropertyName("ownerUsername")
    protected String ownerUsername;

    @PropertyName("comments")
    protected List<Comment> comments;

    @PropertyName("likes")
    protected Set<String> likes; //Store user uid of who like this status

    public NewsFeedItem() {

    }

    protected NewsFeedItem(int likeCount, long timestamp, String ownerUid, String ownerUsername, List<Comment> comments, Set<String> likes) {
        this.ownerUid = ownerUid;
        this.ownerUsername = ownerUsername;
        this.likeCount = likeCount;
        this.timestamp = timestamp;
        this.comments = comments;
        this.likes = likes;
    }

    public int getLikeCount() {
        return likeCount;
    }

    public void setLikeCount(int likeCount) {
        this.likeCount = likeCount;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public String getOwnerUid() {
        return ownerUid;
    }

    public void setOwnerUid(String ownerUid) {
        this.ownerUid = ownerUid;
    }

    public String getOwnerUsername() {
        return ownerUsername;
    }

    public void setOwnerUsername(String ownerUsername) {
        this.ownerUsername = ownerUsername;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    public Set<String> getLikes() {
        return likes;
    }

    public void setLikes(Set<String> likes) {
        this.likes = likes;
    }
}

然后我在状态模型中将其子类化:

@IgnoreExtraProperties
public class Status extends NewsFeedItem {
    @PropertyName("content")
    protected String content;

    @PropertyName("photo")
    protected String photoUrl;

    public Status() {
        //Required for deserialize
    }

    public Status(String ownerUid, String ownerUsername, String content, String photoUrl, int likeCount, long timestamp, List<Comment> comments, Set<String> likes) {
        super(likeCount, timestamp, ownerUid, ownerUsername, comments, likes);
        this.content = content;
        this.photoUrl = photoUrl;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }
}

将数据推送到Firebase的代码:

@Override
public void pushStatusToFirebase(Status status) {
    database.getReference("status").push().setValue(status);
}

但当我向Firebase推送like_numberlikeCount一起显示时,这样:

enter image description here

它也发生在我的所有模型类中。请帮帮我。

2 个答案:

答案 0 :(得分:1)

尝试用@PropertyName标记你的getter而不是字段。可能有效的另一种选择 - 用@Exclude标记你的吸气剂。

答案 1 :(得分:1)

要解决此问题,您需要将所有字段public设置为现在不受保护,否则注释将无效。

现在,注释会考虑字段名称以及要序列化的getter / setter名称。你有这个问题,因为字段以及getter / setter被序列化了,这就是为什么会产生重复的结果。

因此,在字段名称上使用注释是公共的,并忽略getter / setter。这将解决您的问题。您的数据将使用您想要的属性名称进行正确序列化,并且也不会重复。