nhoxbypass
nhoxbypass

Reputation: 10152

Duplicated attribute in Firebase

I have a model NewsFeedItem like this:

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

Then I subclass it in Status model:

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

The code pushing data to Firebase:

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

But when I push to Firebase the like_number and likeCount display together like this:

enter image description here

It also happen to all of my model class. Please help me.

Upvotes: 0

Views: 215

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138824

To solve this, you need to make all your fields public and not protected as they are now, otherwise the annotations will not work.

Now, the annotation takes into account both the field name as well as the getter/setter names to serialize. You have this problem because the fields as well as the getter/setters were getting serialized and that's why are resulting duplicates.

So use the annotation on the field name which are public and ignore the getter/setters. This will solve your problem. Your data will be properly serialized with the property name you want and there will be no duplicates as well.

Upvotes: 1

algrid
algrid

Reputation: 5954

Try to mark with @PropertyName your getters instead of fields. Another option that may be working - mark with @Exclude your getters.

Upvotes: 1

Related Questions