java - How to delete firebase data after "n" days -
i want delete old data friebase. knew question asked lot here, have still hard time make work.
i try solution found here:
firebase chat - removing old messages
but ist deprecated.
so try way:
childeventlistener childeventlistener = new childeventlistener() { @override public void onchildadded(datasnapshot datasnapshot, string s) { calendar calendar = calendar.getinstance(); calendar.add(calendar.day_of_month, -30); log.d(tag, "onchildadded:" + datasnapshot.getkey()); string key = datasnapshot.getkey(); marker retrievemarker =datasnapshot.getvalue(marker.class); calendar t3 = new gregoriancalendar (c.get(calendar.year), c.get(calendar.month), retrievemarker.getsavedate()); int date1= calendar.get(calendar.date); int date2= t3.get(calendar.date); log.d("date1",""+date1); log.d("date2",""+date2); if( date1 == date2 ){ myref.child(key).setvalue(null); }
for explanation: in firebase save object named marker, object got variable store savedate, date object saved in firebase.
int date = c.get(calendar.date); marker.setsavedate(date);
i want delete object in firebase after 30 days. try subtract 30 days date today want compare savedate object if equals delete object firebase.
for substraction refer following answer:
how subtract x days date using java calendar?
but dosen´t work. if add object today firebase 2 dates allways equal. guess subtraction dosen´t work.
what doing wrong?
say have data structure nodes line this:
-kitqnxlqzqolnucb9sjaddclose time: "thu apr 28 17:12:05 pdt 2016" timestamp: 1461888725444
each such node has timestamp
property indicates when created. preferably you'd set property using server timestamp.
with data structure, can build query returns items older 30 days , removes them:
long cutoff = new date().gettime() - timeunit.milliseconds.convert(30, timeunit.days); query olditems = ttlref.orderbychild("timestamp").endat(cutoff); olditems.addlistenerforsinglevalueevent(new valueeventlistener() { @override public void ondatachange(datasnapshot snapshot) { (datasnapshot itemsnapshot: snapshot.getchildren()) { itemsnapshot.getref().removevalue(); } } @override public void oncancelled(databaseerror databaseerror) { throw databaseerror.toexception(); } });
Comments
Post a Comment