如何在“n”后删除firebase数据天 [英] How to delete firebase data after "n" days
问题描述
我在这里找到这个解决方案:
$ b
但这个不推荐使用。
所以我试试这样:
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();
标记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);
$ b $ p
在我的Firebase中,一个名为Marker的对象,这个对象有一个变量来存储保存的对象在Firebase中保存的日期。 int date = c.get(Calendar.DATE);
marker.setSavedate(date);
我想在30天后删除Firebase中的对象。所以我尝试从Date Date减去30天比我想比较这与从Object的savedate,如果它等于我将从Firebase删除对象。
对于减法我参考以下答案:
但是它dosen'd工作。如果我今天添加一个对象到Firebase,两个日期是相等的。
所以我想减法不工作。
我做错了什么?
解决方案一个带节点的数据结构如下:
-KItqNxLqzQoLnUCb9sJaddclose
time:Thu Apr 28 17:12:05 PDT 2016
timestamp:1461888725444
每个这样的节点都有一个时间戳
属性,指明它的创建时间。您最好使用服务器时间戳设置此属性 。
使用这个数据结构,您可以轻松构建一个只返回超过30天的项目并将其删除的查询:
$ b $ b
long cutoff = new Date()。getTime() - TimeUnit.MILLISECONDS.convert(30,TimeUnit.DAYS);
Query oldItems = ttlRef.orderByChild(timestamp)。endAt(cutoff);
oldItems.addListenerForSingleValueEvent(new DataEventListener(){
@Override
public void onDataChange(DataSnapshot snapshot){
for(DataSnapshot itemSnapshot:snapshot.getChildren()){
itemSnapshot.getRef()。removeValue();
}
}
$ b $ @覆盖
public void onCancelled(DatabaseError databaseError){
throw databaseError。 toException();
}
});
I want do delete some old data from my Friebase. I knew this question asked a lot here, but i have still a hard time do make it work.
I try this solution found here:
Firebase chat - removing old messages
but this ist deprecated.
so i try it this 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 my Firebase i save a object named Marker, this object got a variable to store the savedate, the date where the object was saved in the Firebase.
int date = c.get(Calendar.DATE);
marker.setSavedate(date);
I want to delete the object in the Firebase after 30 days. So i try to subtract 30 days from the Date Today than i want to compare this with the savedate from the Object if it equals i will delete the object from the Firebase.
For the Substraction i refer to the following answer:
How to subtract X days from a date using Java calendar?
But it dosen´t work. If i add a Object today to the Firebase the two dates are allways equal.
So i guess the subtraction dosen´t work.
What am I doing wrong?
解决方案 Say that you have a data structure with nodes line this:
-KItqNxLqzQoLnUCb9sJaddclose
time: "Thu Apr 28 17:12:05 PDT 2016"
timestamp: 1461888725444
Each such node has a timestamp
property that indicates when it was created. Preferably you'd set this property using Server Timestamp.
With this data structure, you can easily build a query that returns only the items older than 30 days and 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) {
for (DataSnapshot itemSnapshot: snapshot.getChildren()) {
itemSnapshot.getRef().removeValue();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
这篇关于如何在“n”后删除firebase数据天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!