I am learning about swift coding and firebase. I am trying out this code which in theory does make sense however I do not achieve the desired result. It seems as though my math is wrong however, I think I may be formatting the code incorrectly.
Essentially, a post is saved to the firebase rtd with a timestamp. I am wanting for the post to be deleted if a minute has passed after the post has been uploaded.
let postFinally = Posts(timeStamp: timeStamp as! Double?)
let cutTime = 1.0 * 60.0 * 1000.0
let postTime = postFinally.timeStamp as Any as! Double
if postTime > postTime cutTime {
print("delete")
} else {
print("not delete")
}
// Timestamp derived from the firebase database. Ie, 1633590010833.0
Am I formatting the code wrong? It always returns 'do not delete' even after the desired time.
Any help would be much appreciated! Thank you
Edit Updated Equation However Does Not Seem To Work. I choose the cut time as a hour however, it still executes 'delete' right away.
let postFinally = Posts(timeStamp: timeStamp as! Double?)
let cutTime = 1.0 * 60.0 * 60.0 * 1000.0
let postTime = postFinally.timeStamp as Any as! Double
if postTime < postTime cutTime {
print("delete")
} else {
print("not delete")
}
// Timestamp derived from the firebase database. Ie, 1633590010833.0
CodePudding user response:
Here check it
if postFinally.timeStamp! > postFinally.timeStamp! cutTime {
print("delete")
}
You have added something in postFinally.timeStamp, so how postFinally.timeStamp is greater than postFinally.timeStamp cutTime ?
postFinally.timeStamp cutTime > postFinally.timeStamp
So i think your code will execute else block.
OR
You can get the posted time as string, then you can easily get time difference in seconds. Here is the example
guard let postedDate = timeString.toDate() else { return ""}
let timeDifferenceSecond = Date().timeIntervalSince(msgDate)