Check and compare times of messages in Firebase Swift
what I am trying to do is I have a messages app made in Swift and I'm using Firebase as my database. What I'm trying to do is to have the functionality of disappearing messages like in Snapchat; however, I'm not exactly sure how I should to that. I have a timestamp on the message in the database, but I'm not sure how to use it. This is what I'm trying to do, I just don't know the exact code for it.
Here is a picture of what my database structure looks like. Let me know if you need any more information. Thank you

swift
|
show 4 more comments
what I am trying to do is I have a messages app made in Swift and I'm using Firebase as my database. What I'm trying to do is to have the functionality of disappearing messages like in Snapchat; however, I'm not exactly sure how I should to that. I have a timestamp on the message in the database, but I'm not sure how to use it. This is what I'm trying to do, I just don't know the exact code for it.
Here is a picture of what my database structure looks like. Let me know if you need any more information. Thank you

swift
You need to use a(NS)DateFormatterto transformdatewhich is aStringinto a(NS)Dateobject. Quickly done, thedateFormatseems to beyyyyMMddHHmmss. But since it's in that specific format, you can also transform aDate()into thatStringusing that format and compareString(except if you want a "expires in n seconds" for instance).
– Larme
Nov 22 '18 at 22:57
@Larme would you mind giving me a code example and how do I check the date and compare it so that after 24 hrs all the messages will disappear?
– Jaqueline
Nov 23 '18 at 4:21
Do you want to delete the message from the database as it expires or you just don't want to display it?
– Alexander
Dec 2 '18 at 7:32
@Alexander yes I’d like to delete it from the database
– Jaqueline
Dec 2 '18 at 7:34
1
@Jaqueline You probably want to set up a cron job. There is a great tutorial for firebbase. firebase.googleblog.com/2017/03/…
– Alexander
Dec 2 '18 at 8:01
|
show 4 more comments
what I am trying to do is I have a messages app made in Swift and I'm using Firebase as my database. What I'm trying to do is to have the functionality of disappearing messages like in Snapchat; however, I'm not exactly sure how I should to that. I have a timestamp on the message in the database, but I'm not sure how to use it. This is what I'm trying to do, I just don't know the exact code for it.
Here is a picture of what my database structure looks like. Let me know if you need any more information. Thank you

swift
what I am trying to do is I have a messages app made in Swift and I'm using Firebase as my database. What I'm trying to do is to have the functionality of disappearing messages like in Snapchat; however, I'm not exactly sure how I should to that. I have a timestamp on the message in the database, but I'm not sure how to use it. This is what I'm trying to do, I just don't know the exact code for it.
Here is a picture of what my database structure looks like. Let me know if you need any more information. Thank you

swift
swift
edited Nov 23 '18 at 1:38
Frank van Puffelen
227k28372397
227k28372397
asked Nov 22 '18 at 22:55
Jaqueline
64113
64113
You need to use a(NS)DateFormatterto transformdatewhich is aStringinto a(NS)Dateobject. Quickly done, thedateFormatseems to beyyyyMMddHHmmss. But since it's in that specific format, you can also transform aDate()into thatStringusing that format and compareString(except if you want a "expires in n seconds" for instance).
– Larme
Nov 22 '18 at 22:57
@Larme would you mind giving me a code example and how do I check the date and compare it so that after 24 hrs all the messages will disappear?
– Jaqueline
Nov 23 '18 at 4:21
Do you want to delete the message from the database as it expires or you just don't want to display it?
– Alexander
Dec 2 '18 at 7:32
@Alexander yes I’d like to delete it from the database
– Jaqueline
Dec 2 '18 at 7:34
1
@Jaqueline You probably want to set up a cron job. There is a great tutorial for firebbase. firebase.googleblog.com/2017/03/…
– Alexander
Dec 2 '18 at 8:01
|
show 4 more comments
You need to use a(NS)DateFormatterto transformdatewhich is aStringinto a(NS)Dateobject. Quickly done, thedateFormatseems to beyyyyMMddHHmmss. But since it's in that specific format, you can also transform aDate()into thatStringusing that format and compareString(except if you want a "expires in n seconds" for instance).
– Larme
Nov 22 '18 at 22:57
@Larme would you mind giving me a code example and how do I check the date and compare it so that after 24 hrs all the messages will disappear?
– Jaqueline
Nov 23 '18 at 4:21
Do you want to delete the message from the database as it expires or you just don't want to display it?
– Alexander
Dec 2 '18 at 7:32
@Alexander yes I’d like to delete it from the database
– Jaqueline
Dec 2 '18 at 7:34
1
@Jaqueline You probably want to set up a cron job. There is a great tutorial for firebbase. firebase.googleblog.com/2017/03/…
– Alexander
Dec 2 '18 at 8:01
You need to use a
(NS)DateFormatter to transform date which is a String into a (NS)Date object. Quickly done, the dateFormat seems to be yyyyMMddHHmmss. But since it's in that specific format, you can also transform a Date() into that String using that format and compare String (except if you want a "expires in n seconds" for instance).– Larme
Nov 22 '18 at 22:57
You need to use a
(NS)DateFormatter to transform date which is a String into a (NS)Date object. Quickly done, the dateFormat seems to be yyyyMMddHHmmss. But since it's in that specific format, you can also transform a Date() into that String using that format and compare String (except if you want a "expires in n seconds" for instance).– Larme
Nov 22 '18 at 22:57
@Larme would you mind giving me a code example and how do I check the date and compare it so that after 24 hrs all the messages will disappear?
– Jaqueline
Nov 23 '18 at 4:21
@Larme would you mind giving me a code example and how do I check the date and compare it so that after 24 hrs all the messages will disappear?
– Jaqueline
Nov 23 '18 at 4:21
Do you want to delete the message from the database as it expires or you just don't want to display it?
– Alexander
Dec 2 '18 at 7:32
Do you want to delete the message from the database as it expires or you just don't want to display it?
– Alexander
Dec 2 '18 at 7:32
@Alexander yes I’d like to delete it from the database
– Jaqueline
Dec 2 '18 at 7:34
@Alexander yes I’d like to delete it from the database
– Jaqueline
Dec 2 '18 at 7:34
1
1
@Jaqueline You probably want to set up a cron job. There is a great tutorial for firebbase. firebase.googleblog.com/2017/03/…
– Alexander
Dec 2 '18 at 8:01
@Jaqueline You probably want to set up a cron job. There is a great tutorial for firebbase. firebase.googleblog.com/2017/03/…
– Alexander
Dec 2 '18 at 8:01
|
show 4 more comments
1 Answer
1
active
oldest
votes
I would suggest managing your timestamps in Firebase by the metric of milliseconds since January 1st, 1970, by using NSDate().timeIntervalSince1970 * 1000 in Swift and then Date().getTime(); in your cron job written in JavaScript. You can store let date = Int64(NSDate().timeIntervalSince1970 * 1000) under your date node in firebase for each of your posts when they are created. Then you can create a cron job using Firebase Functions and Google Cloud that runs every hour that queries the posts and deletes posts with date timestamps more than a day old.
Here is a great tutorial on how get started with cron: https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html
Your cron job will need to be coded in node.js and will look something like this:
// 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();
});
});
});
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438848%2fcheck-and-compare-times-of-messages-in-firebase-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would suggest managing your timestamps in Firebase by the metric of milliseconds since January 1st, 1970, by using NSDate().timeIntervalSince1970 * 1000 in Swift and then Date().getTime(); in your cron job written in JavaScript. You can store let date = Int64(NSDate().timeIntervalSince1970 * 1000) under your date node in firebase for each of your posts when they are created. Then you can create a cron job using Firebase Functions and Google Cloud that runs every hour that queries the posts and deletes posts with date timestamps more than a day old.
Here is a great tutorial on how get started with cron: https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html
Your cron job will need to be coded in node.js and will look something like this:
// 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();
});
});
});
add a comment |
I would suggest managing your timestamps in Firebase by the metric of milliseconds since January 1st, 1970, by using NSDate().timeIntervalSince1970 * 1000 in Swift and then Date().getTime(); in your cron job written in JavaScript. You can store let date = Int64(NSDate().timeIntervalSince1970 * 1000) under your date node in firebase for each of your posts when they are created. Then you can create a cron job using Firebase Functions and Google Cloud that runs every hour that queries the posts and deletes posts with date timestamps more than a day old.
Here is a great tutorial on how get started with cron: https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html
Your cron job will need to be coded in node.js and will look something like this:
// 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();
});
});
});
add a comment |
I would suggest managing your timestamps in Firebase by the metric of milliseconds since January 1st, 1970, by using NSDate().timeIntervalSince1970 * 1000 in Swift and then Date().getTime(); in your cron job written in JavaScript. You can store let date = Int64(NSDate().timeIntervalSince1970 * 1000) under your date node in firebase for each of your posts when they are created. Then you can create a cron job using Firebase Functions and Google Cloud that runs every hour that queries the posts and deletes posts with date timestamps more than a day old.
Here is a great tutorial on how get started with cron: https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html
Your cron job will need to be coded in node.js and will look something like this:
// 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();
});
});
});
I would suggest managing your timestamps in Firebase by the metric of milliseconds since January 1st, 1970, by using NSDate().timeIntervalSince1970 * 1000 in Swift and then Date().getTime(); in your cron job written in JavaScript. You can store let date = Int64(NSDate().timeIntervalSince1970 * 1000) under your date node in firebase for each of your posts when they are created. Then you can create a cron job using Firebase Functions and Google Cloud that runs every hour that queries the posts and deletes posts with date timestamps more than a day old.
Here is a great tutorial on how get started with cron: https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html
Your cron job will need to be coded in node.js and will look something like this:
// 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();
});
});
});
edited Dec 5 '18 at 21:00
answered Dec 5 '18 at 20:53
hackerman58888
1729
1729
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438848%2fcheck-and-compare-times-of-messages-in-firebase-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You need to use a
(NS)DateFormatterto transformdatewhich is aStringinto a(NS)Dateobject. Quickly done, thedateFormatseems to beyyyyMMddHHmmss. But since it's in that specific format, you can also transform aDate()into thatStringusing that format and compareString(except if you want a "expires in n seconds" for instance).– Larme
Nov 22 '18 at 22:57
@Larme would you mind giving me a code example and how do I check the date and compare it so that after 24 hrs all the messages will disappear?
– Jaqueline
Nov 23 '18 at 4:21
Do you want to delete the message from the database as it expires or you just don't want to display it?
– Alexander
Dec 2 '18 at 7:32
@Alexander yes I’d like to delete it from the database
– Jaqueline
Dec 2 '18 at 7:34
1
@Jaqueline You probably want to set up a cron job. There is a great tutorial for firebbase. firebase.googleblog.com/2017/03/…
– Alexander
Dec 2 '18 at 8:01