I want to delete apple in breakfast separately, but the above method will delete all apples, regardless of whether it is breakfast, lunch, or dinner. Is there any solution? Thank you
Asked
Active
Viewed 52 times
-1

Frank van Puffelen
- 565,676
- 79
- 828
- 807

Henry Zhang
- 13
- 1
-
1Please don't post screenshots of your code, or other textual content. Instead post the actual text, and use the formatting tools of Stack Overflow to mark it up. – Frank van Puffelen Nov 07 '20 at 15:22
-
Thank you very much, it was so helpful to me – Henry Zhang Nov 08 '20 at 01:20
1 Answers
0
There is no way to add this second condition to the query, as Firebase Realtime Database can only query on one property. See Query based on multiple where clauses in Firebase
If you want to only delete a subset of the items returned by the query, you'll have to check for the addition condition in your loop:
for (DataSnapshot data: snapshot.getChildren()) {
if ("apple".equals(data.child("foodname").getValue(String.class))) {
data.getRef().removeValue();
}
}

Frank van Puffelen
- 565,676
- 79
- 828
- 807