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 :



Database picture



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 ?






equalTo("01/10/2018")....

– Mathias
Sep 16 '18 at 21:44






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

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế