3

In my Android application I am inserting a VideoEntity objects in firebase database this way:

@Override
    protected void onCreate(Bundle savedInstanceState){

        Log.d(TAG, " onCreate(Bundle) - Ini ");
        super.onCreate(savedInstanceState);

        database =  FirebaseDatabase.getInstance();
        String videoId = "";

        for(VideoEntity video: videosList) {
            videoId = video.getId();
            DatabaseReference mRef =  database.getReference().child("Videos").push();
            mRef.setValue(video);
        }

this the VideoEntity class:

 package com.app.entities;

import android.os.Parcel;
import android.os.Parcelable;

import java.io.Serializable;

public class VideoEntity implements Parcelable{

    private String id;
    private String title;
    private String description;
    private String thumbnailURL;
    private String category;



    public VideoEntity() {

    }

    public VideoEntity(Parcel in) {
        id = in.readString();
        title = in.readString();
        description = in.readString();
        thumbnailURL = in.readString();
        category = in.readString();

    }



    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getThumbnailURL() {
        return thumbnailURL;
    }

    public void setThumbnailURL(String thumbnail) {
        this.thumbnailURL = thumbnail;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCategory() {return category;}

    public void setCategory(String category) { this.category = category;}

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(id);
        dest.writeString(title);
        dest.writeString(description);
        dest.writeString(thumbnailURL);
        dest.writeString(category);
    }

    public static final Creator<VideoEntity> CREATOR = new Creator<VideoEntity>() {
        @Override
        public VideoEntity createFromParcel(Parcel in) {
            return new VideoEntity(in);
        }

        @Override
        public VideoEntity[] newArray(int size) {
            return new VideoEntity[size];
        }
    };
}

The problem is that everytime I start this activity the same objects are inserted in the database, so i have duplicate data in the database.

Is there anyway to avoid this ?

rainman
  • 2,551
  • 6
  • 29
  • 43
  • Please explain much more about what youre trying to do, what you expect to happen, and what exactly is happening differently. "Same data is inserted?" What do you mean? Where does this "videolist" array/object come from? – Nerdy Bunz Sep 18 '16 at 01:48
  • I do not think that that is realy relevant to know where the videolist come from, but i edited my question. – rainman Sep 18 '16 at 02:00

1 Answers1

9

If your data has a natural key, store it under its natural key. In your case the videos have an id field, which likely is unique already. So instead of storing the videos under a push ID, store them under their existing ID:

DatabaseReference mRef =  database.getReference().child("Videos").child(video.getId());
mRef.setValue(video);

That way the next time you run the app, it will just write the same data in the same location again.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you again, it is exactly what i was looking for – rainman Sep 18 '16 at 11:02
  • @Frank, what if the collection of data in firebase is super big? is that method still working? I mean, Fiirebase Performance would not be affected by the fact that the new data is being saved, but the data that already has an ID is overwritten? – Daniel Henao Jul 30 '17 at 00:27