我想做的是我有一个用 Swift 制作的消息应用程序,我使用 Firebase 作为我的数据库。我想做的是像 Snapchat 一样具有消失消息的功能;但是,我不确定我应该怎么做。我在数据库中的消息上有一个时间戳,但我不确定如何使用它。 This是我想要做的,我只是不知道它的确切代码。
这是我的数据库结构的图片。如果您需要更多信息,请告诉我。谢谢
最佳答案
我建议通过在 Swift 中使用 NSDate().timeIntervalSince1970 * 1000
以 自 1970 年 1 月 1 日以来的毫秒数 来管理 Firebase 中的时间戳,然后 Date().getTime();
在用 JavaScript 编写的 cron 作业中。您可以在创建每个帖子时将 let date = Int64(NSDate().timeIntervalSince1970 * 1000)
存储在 firebase 中的 date
节点下。然后,您可以使用 Firebase Functions 和 Google Cloud 创建一个 cron 作业,每小时运行一次,查询帖子并删除 date
时间戳超过一天的帖子。
这里有一个关于如何开始使用 cron 的很棒的教程:https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html
您的 cron 作业需要在 node.js
中编码,看起来像这样:
// example cron job after setting everything up in the tutorial
exports.hourly_job =
functions.pubsub.topic('hourly-tick').onPublish((event) => {
// get current date and time
var currentDate = new Date();
console.log("Hourly Deletion Ran at: " + currentDate);
var currentNumMilliseconds = currentDate.getTime();
// remove a days worth of time
var oneDayAgo = currentNumMilliseconds - (3 * 24 * 60 * 1000);
var cutoffDate = new Date(oneDayAgo);
console.log("Query start at date: " + oneDayAgo);
// the firebase database ref where your posts are stored
const ref = admin.database().ref('posts');
// query all posts more than a day old
ref.orderByChild('date').startAt(oneDayAgo).once('value').then(function (snapshot) {
// for each snapshot returned from the query that is older than one day, delete
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
var postObject = childSnapshot.val();
ref.child('key').remove();
});
});
});
关于swift - 在 Firebase Swift 中检查和比较消息的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53438848/