简体   繁体   English

24 小时后自动更改 Firestore 中的变量 - Swift

[英]Automatically change a variable in Firestore after 24 hours - Swift

I would like to create an application in Swift in which the user will be able to temporarily change their data on a Firestore document, and then at the end of 24 hours it will return as before.我想在 Swift 中创建一个应用程序,用户可以在其中临时更改他们在 Firestore 文档上的数据,然后在 24 小时结束时它会像以前一样返回。 For example in a "Name" document, the name "Michael" is present;例如,在“姓名”文档中,存在姓名“迈克尔”; the user of his choice wants to change it to "Andrew" and can do it, but after 24 hours he must return to "Michael".他选择的用户想要将其更改为“Andrew”并且可以做到,但 24 小时后他必须返回“Michael”。 How can I do?我能怎么做?

Suppose the docs the user is changing are in a collection called MyDocs , a reasonable design for this would work as follows:假设用户正在更改的文档位于名为MyDocs的集合中,对此的合理设计将按如下方式工作:

  1. Create a second collection, like MyDocsToRestore , that will maintain copies of the docs the user changes.创建第二个集合,例如MyDocsToRestore ,它将维护用户更改的文档的副本。
  2. Place an update trigger on MyDocs that saves the old doc data along with a restoreAt date when it should be restored.在 MyDocs 上放置一个更新触发器,以保存旧文档数据以及应恢复的restoreAt日期。
exports.updateMyDocs = functions.firestore
    .document('MyDocs/{docId}')
    .onUpdate(async (change, context) => {
      // if we've got a backup, just change the restoreAt time
      const ref = doc(db, 'MyDocsToRestore', context.params.docId);
      const doc = await ref.get();
      const data = doc.exists ?  doc.data() : change.before.data();

      data.restoreAt = new Date();
      data.restoreAt.setDate(data.restoreAt.getDate() + 1);
      
      // save a copy in MyDocsToRestore
      await setDoc(ref, data);
    });
  1. Create a scheduled function that awakes every so often and looks for docs who's restoreAt dates have passed.创建一个定期醒来的 function,它会经常醒来并查找restoreAt日期已过的文档。 ("every so often" means as infrequently as possible, but with acceptable precision relative to 24 hour expiration). (“每隔一段时间”表示尽可能不频繁,但相对于 24 小时到期具有可接受的精度)。 Like this...像这样...
exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun(async (context) => {

  const q = query(collection(db, "MyDocsToRestore"), where("restoreAt", "<=", new Date()));

  const querySnapshot = await getDocs(q);
  const promises = querySnapshot.docs.map(doc => {
    const data = doc.data();
    delete data.restoreAt;
    return update(doc(db, 'MyDocs', doc.id), data);
  });
  return Promise.all(promises);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 24 小时后自动更改 Firestore 中的变量 - Android 中的 Java - Automatically change a variable in Firestore after 24 hours - Java in Android 在过去 24 小时内从 Firestore 中的集合中获取最大文档 - Getting biggest document from a collection in the last 24 hours in Firestore 在 Android 中从 Firestore 获取最近 24 小时的文档 - Get documents of last 24 hours from Firestore in Android Firebase Remote Config A/B 测试显示 24 小时后没有结果 - Firebase Remote Config A/B testing shows no results after 24 hours 条件取决于我想在 Firestore 的 onCompleteListenr 中更改值的变量值,但该侦听器在 if 条件后更改值 - A condition depend on a variable value that I want to change value in onCompleteListenr of Firestore but that listener change value after if condition 在 flutter 中的 Firestore 响应后自动关闭对话框 - Closing dialog automatically, after firestore response in flutter 将小时:分钟转换为秒,其中小时 > 24 - Convert hours:minutes to seconds where hours > 24 使用 read function in Swift 将 firestore 值分配给变量 - Assign firestore value to variable using read function in Swift 计数与前 24 小时的天差 - Day difference of counts from prior 24 hours CloudWatch 无法在 24 小时内可靠地监控单个数据点 - CloudWatch doesn't reliably monitor single datapoint within 24 hours
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM