Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 230
rainman Avatar asked Sep 11 '26 17:09

rainman


1 Answers

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.

like image 146
Frank van Puffelen Avatar answered Sep 13 '26 07:09

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!