Firebase Android,检查是否存在一个对象(带有多个子对象) [英] Firebase Android, checking if an object (with multiple children) exists

查看:69
本文介绍了Firebase Android,检查是否存在一个对象(带有多个子对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase数据库存储用户的报告.每个用户只能提交10个不同的报告.

I am using a Firebase Database to store user’s reports. Each user is allowed to submit only 10 different reports.

在下面的示例中,我们有一个名为"Jasna Kuljancic"的用户,已提交了3个报告.

In the example below we have a user named "Jasna Kuljancic" who has submitted 3 reports.

我的问题是,如何检查用户是否已提交特定报告?

My question is how can you check if a user has submitted a particular report already?

我将必须检查"location","mode","spinnerOne"的值,并将它们与用户刚刚输入的内容进行比较,如果已经存在,则不要添加到数据库中,如果存在,那么添加它.这是我的尝试:

I would have to check the values of "location", "mode", "spinnerOne" and compare them to what the user just entered and if it’s already there then don't add to the database and if it is, then add it. Here is my attempt:

public void checkifentryExistofNot() {
                uniqueID = mDatabaseReference.push().getKey();
                fullname = "Jasna Kuljancic";
                CATEGORY = "Clinical First"); 

                final Report subreport = new Report(SELECTED_LOCATION,
                        locationEditText.getText().toString(),
                        modeEditText.getText().toString(),
                        Integer.parseInt(dateEditText.getText().toString()));

                mDatabaseReference.child("Students Reports").child(fullname).child(CATAGORY).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                int numberofCount = (int) dataSnapshot.getChildrenCount();
                if (numberofCount == 0) {

                    mDatabaseReference.child("Students Reports").child(fullname).child(CATAGORY).child(uniqueID).setValue(subreport);

                }

                if (numberofCount != 0) {

                    for (DataSnapshot child : dataSnapshot.getChildren()) {
                        Report datareports = child.getValue(Report.class);
                        thelocation = datareports.getLocation();
                        themode = datareports.getMode();

                        if (thelocation.equals(locationEditText.getText().toString()) && themode.equals(modeEditText.getText().toString())) {
                            break;
                        } else {
                            mDatabaseReference.child("Students Reports").child(fullname).child(CATAGORY).child(uniqueID).setValue(subreport);
                        }
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

我的解决方案存在的问题是for循环将检查每个报告,并且在每种情况下都将添加或不添加新报告,但这将在现有报告的数量上发生,并且只应执行一次.

The issue with my solution is that the for loop will check over each report and in each case it will either add a new report or not but this will happen over the number of reports that exist and it should only do it once.

换句话说,如果我们有3个条目,并且每次都不存在,则它将提交报告3次(这确实不错).但是,如果它检查了第一个报告,并且确实存在,则循环将退出,因为我在那里有一个中断"(这很好),但是假设第二个报告是将要重复的报告,那么它已经将其添加到数据库中,因为第一项不是重复项.

In other words, if we have 3 entries and each time it does not exist, it will submit the report 3 times (which is not really bad). But if it checks the first report and it does exist the loop will exit since I have a "break" in there (this is good) but let’s say the second report is the one that is going to be a duplicate, it would have already added it to the database because the first entry was not a duplicate.

我觉得我已经做完了,应该有一种更快的方法来做.

I feel like I am over doing this and there should be a much quicker way to do this.

推荐答案

Firebase查询只能按单个属性进行排序/过滤.因此,Firebase中没有WHERE子句.相反,您应该耦合一个名为location_mode_spinnerOne的复合值.您的数据库结构应如下所示:

Firebase queries can only order/filter by a single property. So there is no WHERE clause within Firebase. What should you do instead, is to couple a compound value named location_mode_spinnerOne. Your database structure should look like this:

Firebase-root
   |
   -- Students Reports
         |
         -- Jasna Kuljancic
               |
               -- Clinical First
                     |
                     -- -KuVRQ4OjdfKXCNdLWzb
                            |
                            --- data: 3939393
                            |
                            --- location: "fifififi"
                            |
                            --- mode: "ododododo"
                            |
                            --- spinnerOne: "Asylum Hill Family Clinic"
                            |
                            --- location_mode_spinnerOne: "fifififi_ododododo_Asylum Hill Family Clinic"

您可能已经看到,我为每个特定类别添加了新的复合值location_mode_spinnerOne.这意味着您可以根据此新的location_mode_spinnerOne字段查询Firebase数据库.假设上述数据库结构正确,请使用以下代码:

As you probably see, i have added the new compound value location_mode_spinnerOne for each particular category. This means that you can query your Firebase database according to this new location_mode_spinnerOne field. Assuming that the above database structure is correct, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference categoryRef = rootRef.child("Students Reports").child(fullname).child(CATAGORY);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String searchedText = "fifififi";

        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String location_mode_spinnerOne = ds.child("location_mode_spinnerOne").getValue(String.class);
            if(!location_mode_spinnerOne.contains(searchedText)) {
                categoryRef.child(uniqueID).setValue(subreport);
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
categoryRef.addListenerForSingleValueEvent(eventListener);

我为您提供了一个搜索fifififi关键字的示例. searchedText文本将是用户键入的确切搜索文本.

I gave you an example for searching fifififi keyword. The searchedText text would be the exact searched text typed by the user.

为了更好地理解,我建议您看到

To better understanding, i suggest you see this tutorial.

这篇关于Firebase Android,检查是否存在一个对象(带有多个子对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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