1

enter image description here

above is my firebase node tree. here is my code:

private fun attemptAdd(){
    var focus: View? = null
    var cancel: Boolean = false
    var prefix: String  = prePrefView.text.toString() + prefText.text.toString()
    var sp: SimPrefix

    //checks for the length of the input
    if(prefText.length()<2){
        prefText.error = "invalid length"
        cancel = true
        focus = prefText
    }

    mDatabaseReference.child("simnumbers")
            .orderByChild("simPref")
            .equalTo(prefix)
            .addListenerForSingleValueEvent(object : ValueEventListener{
                override fun onDataChange(dataSnapshot: DataSnapshot) {
                    dataSnapshot.children.forEach {
                        sp = it.getValue(SimPrefix::class.java)!!
                        if(sp.simCard.equals(prefix)){ //checks if there is already a node with the same data
                            prefText.error = "already exists"
                            cancel = true
                            focus = prefText
                        }
                    }
                }

                override fun onCancelled(p0: DatabaseError?) {

                }
            })

    if(cancel){
        focus?.requestFocus()
    }
    else{ //if all is valid, proceed to the next method.
        addPrefix()
    }
}

What I assume here is that it doesn't seem to enter the listener since it always proceeds to the next method allowing the user to enter the same existing data.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
ronan
  • 369
  • 1
  • 4
  • 13
  • [Have you seen this question? The answers here might help you.](https://stackoverflow.com/questions/47893328/checking-if-a-particular-value-exists-in-the-firebase-database?rq=1) – Christan Shane Plaza May 02 '18 at 06:38

2 Answers2

1

I provide java code you can change into kotlin and also put your condition in to onDatachange method..

     Query query1=reference.child(reference.getKey()).child("simnumbers").orderByChild("simPref").equalTo(prefix);
    query1.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // here place your condition
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
0

Try the following to check if data exists:

  mDatabaseReference.child("simnumbers")
        .orderByChild("simPref")
        .equalTo(prefix)
        .addListenerForSingleValueEvent(object : ValueEventListener{
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                    if(dataSnapshot.exists()){
                dataSnapshot.children.forEach {
                    sp = it.getValue(SimPrefix::class.java)!!
                       }
                    }
                }
            }

            override fun onCancelled(p0: DatabaseError?) {

            }
        })

The datasnapshot is at simnumbers, then it will check if that snapshot exists in the database and if the child simPref is equal to the value provided

public boolean exists ()

Returns true if the snapshot contains a non-null value.

more info here:

https://firebase.google.com/docs/reference/android/com/google/firebase/database/DataSnapshot.html#exists()

Community
  • 1
  • 1
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134