I'm creating an IOS Application where I've some discount codes and I would like them to expire after a specific date.
Is there any way I can automatically delete document which has expired?
Example: Let's say I create a discount code which should expire "2019-12-20 12:00", how can I delete this document when the time has passed? Is there any way to do this in Firestore? or can I compare current date with expiring date and remove it if it's older or equals to the time? If yes, how can I do this?
I've tried this so far:
let expiring = data["expiring"] as? String
let dateNow = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm"
let currentDate = dateFormatter.string(from: dateNow)
let date = dateFormatter.date(from:expiring!)
print(date, " ", currentDate)
But when I print it out the "date". returns "nil" and the currentDate returns the correct date. When I get both of them to return the correct value I would like to add an if-statement to check if "date" is older or equal to the correct date and time, and if that's the case, removing it.
Is there any easier way to do this? Like adding something in firestore to detect it automatically?
Do you know how to sort this up? Do you know how I can remove data when the date is older/equal to the expiring date?