0

is there a possibilty to avoid redundance in the firebase database?

I have an app where users can add a first, and a second word. With a button you can get a random word of this two words.

 Random rand = new Random();
            int n = rand.nextInt(9);

            Random rand1 = new Random();
            int m = rand1.nextInt(8);

            String first = sp.getItemAtPosition(n).toString();
            String second = sp2.getItemAtPosition(m).toString();

            out.setText(first + second);

So I get the words from the Spinner (please don't ask why this way)

Ok so now. How can I check, if the random choosen word is already in the database or not?

With an if-else instruction?

The structure looks like this:

{"1stWord" : {
"1stWord" : {
  "-LI125GLPy0-IPAm4GEM" : {
    "name" : "test"
  },

Thanks in advance guys!

Hansi
  • 97
  • 3
  • 13

3 Answers3

2

Firebase has its own unique id as default i think that would be the best way to go cause firebase actually does the checking and assigning, hence that way you reduce on the number of code. so i would advise just leaving it that way. set the unique id at document level.

Yushin
  • 1,684
  • 3
  • 20
  • 36
2

If you want to ensure a certain value is unique in the database, the easiest way to use that value as the key within the node. So if you words are to be unique, create a node words and under that store each word as a key with a dummy value of true. E.g.

"words": { 
  "avoid": true, 
  "redundance": true 
}

Since keys are by definition unique within a collection, this automatically ensures your requirement is met.

To write a new word to this collection, you'd use a transaction:

DatabaseReference wordRef = FirebaseDatabase.getInstance().getReference("words/avoid");
wordRef.runTransaction(new Transaction.Handler() {
    @Override
    public Transaction.Result doTransaction(MutableData mutableData) {
        String word = mutableData.getValue(String.class);
        if (word == null) {
            mutableData.setValue("avoid");
            return Transaction.success(mutableData);
        }
    }

    @Override
    public void onComplete(DatabaseError databaseError, boolean b,
                           DataSnapshot dataSnapshot) {
        // Transaction completed
        Log.d(TAG, "writeWord completed: " + databaseError);
    }
});

Also see some of these previous answers:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1

You need to use a query to check if the value exists in the database:

ref.child("Words").orderByChild("randomWord1").equalTo(second).addListenerForSingleValueEvent(new ValueEventListener() {           
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
         if(dataSnapshot.exists()){
               //it exists
         }
     }

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

Assuming you have this database:

Words
 randomId
   randomWord1: "words"

So here you use orderByChild() to check if this child is equal to the random word. Then you use dataSnapshot.exists() to check if the data that you are trying to retrieve exists in the database.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Thank you for your response. What is "equalTo(second)"? So what is second? I added the database structure in the question at the top. – Hansi Jul 26 '18 at 07:36
  • this is second `String second = sp2.getItemAtPosition(m).toString();` you add it inside equalTo to check if this string is in the database or not @Hansi – Peter Haddad Jul 26 '18 at 07:39
  • Ok. But in this case the second String is just one random word (the random letter because the "m"). But how can I check if the out String (String out = first + second;) is in the database? Is it even possible to check it with that timestamps ? – Hansi Jul 26 '18 at 08:09
  • Yes you asked "How can I check, if the random choosen word is already in the database or not?". And that is what the answer is, if lets say the word is "house", then if you do orderByChild("randomWord1").equalTo("house") it will check if "house" is in the database and then it will check if this datasnapshot already exists in the database @Hansi – Peter Haddad Jul 26 '18 at 08:12
  • Thank you. But unfortunately, in my case it doesn't work. I've added in the if section a ToastText and than I made an else instruction which opens the method who add the word to the database. But in every case - even if the word already exists - the app adds the word to the database. Sorry I'm not a programming genie :D – Hansi Jul 26 '18 at 08:46
  • 1
    use `.addValueEventListener(new ValueEventListener(){` instead of `.addListenerForSingleValueEvent(new ValueEventListener() {` – Peter Haddad Jul 26 '18 at 08:51