【问题标题】:is there a way to delete data from a firebase real time database (using flutter) after 1 day?有没有办法在 1 天后从 firebase 实时数据库(使用 flutter)中删除数据?
【发布时间】:2022-12-10 04:06:28
【问题描述】:

我想添加默认情况下在数据库中保留 1 天然后被删除的数据。除非我设置不同的时间(超过一天)。

【问题讨论】:

  • 这个问题的细节很少,但我会说你应该看看使用 Firebase 云函数来实现它。没有执行此操作的 RTDB 机制。
  • 您可以使用 firebase 云功能来完成该任务。

标签: flutter firebase-realtime-database


【解决方案1】:

您可以创建类似以下方法的内容,您需要将其上传到 Firebase Cloud Function

const CUT_OFF_TIME = 24 * 60 * 60 * 1000; // 2 小时,以毫秒为单位。

exports.deleteOldMessages = functions.database.ref('[path]').onWrite(async (change) => {
    const ref = change.after.ref.parent; // reference to the parent
    const now = Date.now();
    const cutoff = (now - CUT_OFF_TIME) / 1000; // convert to seconds
    const oldItemsQuery = ref.orderByChild('seconds').endAt(cutoff);
    const snapshot = await oldItemsQuery.once('value');
    // create a map with all children that need to be removed
    const updates = {};
    snapshot.forEach(child => {
        updates[child.key] = null;
    });
    // execute all updates in one go and return the result to end the function
    return ref.update(updates);
});

您也可以查看以下链接以供参考。

souce

【讨论】:

    猜你喜欢
    • 2019-08-13
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2019-08-14
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多