rainman
rainman

Reputation: 2649

How to avoid duplicate data in Firebase Database

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 ?

Upvotes: 3

Views: 5422

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

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.

Upvotes: 9

Related Questions