Firebase Android, checking if an object (with multiple children) exists

前端 未结 3 1764
野性不改
野性不改 2021-01-24 09:20

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

In the example below we have a user named “Jasna Kulja

相关标签:
3条回答
  • 2021-01-24 09:34

    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"
    

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

    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.

    0 讨论(0)
  • 2021-01-24 09:52

    dataSnapshot.getChildrenCount() it will show you total number of children's.! All you have to do is make a Model class save your value in objects and while editing check if object has same data then don't update data and if its different then update data.!

    0 讨论(0)
  • 2021-01-24 09:55

    There're two approaches to this problem. I think for the first approach you've quite figured it how to do so. Only missing thing is to separate the sequential actions of [a] validating the uniqueness of the report and [b] adding the object to the database. There might be a button press for adding the report. I suggest, on the button press get the data snapshot of the required table. As the uniqueness of the report is restricted a single user, fetch all the reports for a single user. Then simply compare the target report with the fetched reports and proceed accordingly. If unique then add or report a duplicate otherwise. The difference (w.r.t your code) is no event listener would be attached. And I think your code processing inline with Firebase data. Here you'll get the data dump, compare it and then update it.

    Another approach is to leverage the rules provided by Firebase. Basically, it's same as the first approach, but all work is done by firebase. Here's the code for inserting only unique usernames to Firebase:

     "usernames": {
      "$usernameid": {
      ".read": "auth != null",
        ".write": "auth != null  && (!data.exists() || !newData.exists())"
      }
     }
    

    In the context of your question, you check the uniqueness of the report based on three values. Add the rules for all three values. This would allow inserting the unique reports for each user. But be sure to add the rules correctly as for the scope of the rule must be for a single user, not for all the reports in the database.

    0 讨论(0)
提交回复
热议问题