如何避免Firebase数据库中的重复数据 [英] How to avoid duplicate data in Firebase Database

查看:45
本文介绍了如何避免Firebase数据库中的重复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用程序中,我正在通过以下方式在Firebase数据库中插入VideoEntity对象:

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

这是VideoEntity类:

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 ?

推荐答案

如果数据具有自然键,则将其存储在自然键下.在您的情况下,视频具有一个 id 字段,该字段可能已经是唯一的.因此,与其将视频存储在推送ID下,不如将其存储在现有ID下:

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.

这篇关于如何避免Firebase数据库中的重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆