Reputation: 31
getReference ("Posts"). child (postKey) .child ("date") where the date is written in "date" (which will be in the future, for example, April 30, 2020 in String format). I want to write a program that will take place "Post" to get the whole date, and if the current date and date from "Post" (currentDate == date ("Post")) then delete this post. How to get everything from the list for comparison?
I added the code where I add "Post". I get the date from calendarView so conveniently it is written in Russian language
private void deletePostFromFirebase(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);
Date date = new Date();
String newDate = simpleDateFormat.format(date);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts");
ref.orderByChild("date").equalTo(newDate).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot itemSnapshot: dataSnapshot.getChildren()) {
itemSnapshot.getRef().removeValue();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
Upvotes: 1
Views: 46
Reputation: 80914
If you want to delete the date according to a condition, then you can do the following:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts");
ref.orderByChild("date").equalTo(date).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
String key = ds.getKey();
ref.child(key).child("date").removeValue();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Perform a query orderByChild("date").equalTo(date)
then you can iterate and retrieve the key
and delete the child date
.
Upvotes: 1