如何在“n"之后删除firebase数据天 [英] How to delete firebase data after "n" days

查看:25
本文介绍了如何在“n"之后删除firebase数据天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的 Firebase 中删除一些旧数据,但我很难让它发挥作用.

我在此处尝试了此解决方案:Firebase 聊天 - 删除旧消息

但这并没有被弃用.

所以我这样尝试:

ChildEventListener childEventListener = new ChildEventListener() {@覆盖public void onChildAdded(DataSnapshot dataSnapshot, String s) {日历日历 = Calendar.getInstance();日历.添加(日历.DAY_OF_MONTH,-30);Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());String key = dataSnapshot.getKey();标记检索标记 =dataSnapshot.getValue(Marker.class);日历 t3 = 新阳历日历(c.get(Calendar.YEAR),c.get(Calendar.MONTH),检索标记.getSavedate());int date1= calendar.get(Calendar.DATE);int date2= t3.get(Calendar.DATE);Log.d("date1",""+date1);Log.d("date2",""+date2);如果(日期1 == 日期2 ){myRef.child(key).setValue(null);}

解释:在我的 Firebase 中,我保存了一个名为 Marker 的对象,这个对象有一个变量来存储保存日期,即对象在 Firebase 中保存的日期.

int date = c.get(Calendar.DATE);标记.setSavedate(日期);

我想在 30 天后删除 Firebase 中的对象.所以我尝试从今天的日期中减去 30 天,然后我想将它与对象中的保存日期进行比较,如果它等于我将从 Firebase 中删除该对象.

对于减法,我参考了以下问题的答案:如何使用 Java 日历从日期中减去 X 天?

但它不起作用.如果我今天向 Firebase 添加一个对象,则两个日期始终相等.所以我猜减法不起作用.

我做错了什么?

解决方案

假设你有一个包含节点的数据结构:

-KItqNxLqzQoLnUCb9sJaddclose时间:2016 年 4 月 28 日星期四 17:12:05 PDT"时间戳:1461888725444

每个这样的节点都有一个 timestamp 属性,表明它是何时创建的.您最好使用服务器时间戳设置此属性.

使用此数据结构,您可以轻松构建查询,仅返回超过 30 天的项目并将其删除:

long cutoff = new Date().getTime() - TimeUnit.MILLISECONDS.convert(30, TimeUnit.DAYS);查询 oldItems = ttlRef.orderByChild("timestamp").endAt(cutoff);oldItems.addListenerForSingleValueEvent(new ValueEventListener() {@覆盖公共无效 onDataChange(数据快照快照){for (DataSnapshot itemSnapshot: snapshot.getChildren()) {itemSnapshot.getRef().removeValue();}}@覆盖public void onCancelled(DatabaseError databaseError) {抛出 databaseError.toException();}});

I want do delete some old data from my Firebase, and I have a hard time do make it work.

I tried this solution found here: Firebase chat - removing old messages

but this isn't 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 Subtraction I refer to an answer from the following question: How to subtract X days from a date using Java calendar?

But it doesn´t work. If I add a Object today to the Firebase the two dates are always equal. So I guess the subtraction doesn´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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆