Delete multiple datas sharing a same child from Firebase - Java - orderByKey
Delete multiple datas sharing a same child from Firebase - Java - orderByKey
I have the following Firebase Database :
I need to delete all the entries/database objects sharing the same "date_cours" type.
I tried the following method to delete all the entries sharing the same date_cours "10/09/2018", for example :
private void Delete_CR_Lessons(Date date)
final String date_a_supprimer_string = DateFormat.getDateInstance(DateFormat.SHORT).format(date);
DatabaseReference drTest = FirebaseDatabase.getInstance().getReference("cours");
drTest.child("date_cours").orderByKey().equalTo("10/09/2018")
.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
Log.i("Tag", "test1");
for (DataSnapshot postsnapshot :dataSnapshot.getChildren())
Log.i("Tag", "test2");
String key = postsnapshot.getKey();
dataSnapshot.getRef().removeValue();
@Override
public void onCancelled(DatabaseError databaseError)
Log.w("TAG: ", databaseError.getMessage());
);
//fin de la methode Delete_CR_Lessons
I have no error during the execution of the method.
In the Logs, I can see my Log "test1" but not the log "test2".
Does anyone know what I am missing ?
What exactly is the value of your dataSnapshot? (Are you actually getting anything with your query?)
– André Kool
Sep 16 '18 at 22:08
@Mathias I have posted you an answer check the code below it contains path fixed note:
orderByChild
– Yupi
Sep 16 '18 at 22:12
orderByChild
@André Kool : Thanks for your comment! A contributor solves the problem. See below reponse if your are interested.
– Mathias
Sep 16 '18 at 22:16
1 Answer
1
You are providing wrong path and than you are trying to delete wrong datasnapshot
value for example try to use: postsnapshot.getRef().removeValue();
instead of dataSnapshot.getRef().removeValue();
because dataSnapshot
doesn't point to the value which you want to delete. That is why you used for
loop to get all value nodes from your database. Check code below:
datasnapshot
postsnapshot.getRef().removeValue();
dataSnapshot.getRef().removeValue();
dataSnapshot
for
DatabaseReference drTest = FirebaseDatabase.getInstance().getReference("cours");
drTest.orderByChild("date_cours").equalTo("01/10/2018")
.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
Log.i("Tag", "test1");
for (DataSnapshot postsnapshot :dataSnapshot.getChildren())
Log.i("Tag", "test2");
String key = postsnapshot.getKey();
postsnapshot.getRef().removeValue();
@Override
public void onCancelled(DatabaseError databaseError)
Log.w("TAG: ", databaseError.getMessage());
);
It works !! Thanks a lot !
– Mathias
Sep 16 '18 at 22:16
You are welcome ;)
– Yupi
Sep 16 '18 at 22:17
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
equalTo("01/10/2018")....
– Mathias
Sep 16 '18 at 21:44