在插入Firebase之前检查重复项 [英] Check for duplicates before insertion in Firebase

查看:36
本文介绍了在插入Firebase之前检查重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Firebase的新手,我试图将其实现到Android应用程序中.到现在为止一切都很好.在插入记录之前,我必须检查记录中是否有重复项,即使在网上观看了大量教程之后,我也无法弄清楚.

I am new to Firebase and I trying to implement it into an Android application. It was all going well until now. I have to check the records for duplicates before inserting them and even after the tons of tutorials I watched online I can't figure it out.

我尝试使用规则,但是显然我无法以它们能起作用的方式写下来.我尝试使用push(),但这也不起作用.

I tried with rules, but apparently I was not able to write them down in a way they would work. I tried with push(), but this was not working either.

目前,这是我拥有的代码:

At the moment this is the code that I have:

private void updateDatabase(String year, String courseOfStudent, String workshopGroup, String day, String time, String lecture){
    try{
        mDatabase.child("student-timetables").child("course-of-student").setValue(courseOfStudent);
        mDatabase.child("student-timetables").child(courseOfStudent).child("year").setValue(year);
        mDatabase.child("student-timetables").child(courseOfStudent).child(year).child("workshop-group").setValue(workshopGroup);
        mDatabase.child("student-timetables").child(courseOfStudent).child(year).child(workshopGroup).child("day").setValue(day);
        mDatabase.child("student-timetables").child(courseOfStudent).child(year).child(workshopGroup).child(day).child("time").setValue(time);
        mDatabase.child("student-timetables").child(courseOfStudent).child(year).child(workshopGroup).child(day).child(time).child("lecture").setValue(lecture);}
    catch (Exception e)
    {
        Toast.makeText(getContext(), "Internal error occurred, please try again.", Toast.LENGTH_LONG).show();
    }
}

以及Firebase控制台中的规则:

And the rules from the Firebase console:

 {
  "rules": {
    ".read": "true",
    ".write": "auth != null",
      "course-of-student": {
        "year": {
          "workshop-group": {
            "day":{              
              "time": {
                ".write": "!data.exists()"
              }              
            }
          }
        }
      }
  }
}

到目前为止,如果在特定时间有一次讲座的详细信息,而我尝试为此创建一个新记录,则以前的记录将被覆盖.我想拒绝此消息,而是显示一条消息.

As of now what happens is that if there is a lecture detail at a specific time and I try to create a new record for this time the previous one gets overwritten. I want to reject this and display a message instead.

我在哪里错了?

推荐答案

首先,添加"ListenerForSingleValueEvent",然后再将数据保存在Firebase中(如果不存在). 例如:

First, add "ListenerForSingleValueEvent", and only then save the data in Firebase (if not exist..) For example:

mDatabase.child("student-timetables").child(courseOfStudent).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
     if(!(snapshot.child("year").exist))
    mDatabase.child("student-timetables").child(courseOfStudent).child("year").setValue(year);
   else
   Toast.maketext....."year exist.."

     if(!(snapshot.child(year).child("workshop-group").exist))
    mDatabase.child("student-timetables").child(courseOfStudent).child(year).child("workshop-group").setValue(workshopGroup);
   else
   Toast.maketext....."workshopGroup exist.."
    }
}

这篇关于在插入Firebase之前检查重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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