my goal is to set a timer in one vc when a firestore document is created and fire it in another vc when the timer is up to delete the firestore document.
In my vc where I create the document, I have this block of code in the button that creates the document in firestore:
db.collection("school_users/\(user?.uid)/events").addDocument(data: ["event_name": nameTextField.text, "event_date": dateTextField.text, "event_cost": costTextField.text, "for_grades": gradesTextField.text, "school_id": schoolIDTextF.text, "time_created": Date()]) { (error) in
if error != nil {
self.showError("There was an error trying to add user data to the system.")
} else {
self.setTimerForEventDeletion()
self.dismiss(animated: true, completion: nil)
}
}
The setTimerForEventDeletion()
function contains this :
func setTimerForEventDeletion() {
let date = dateTextField.text!
let dateToDelete = formatter.date(from: date)!
let timer = Timer(fireAt: dateToDelete, interval: 0, target: self, selector: #selector(callDeleteEventFunction), userInfo: nil, repeats: false)
}
The date format is correct as well I already have all that setup. For the objc method, I call another function from a different vc (will explain) in that function.
@objc func callDeleteEventFunction() {
otherVC.deleteEventAtEventTime()
}
The reason I call this function from the other vc is because the other vc is storing the details of the created document, you can't fetch the details of a document that isn't created yet, so I have it in another VC. The function looks like this...
@objc func deleteEventAtEventTime() {
db.document("school_users/\(user?.uid)/events/\(selectedEventDocID!)").delete { (error) in
if let error = error {
print("There was an error deleting the doc: \(error)")
} else {
print("Doc deleted!")
}
}
}
Now before posting this question, I tested it by firing the timer in the same vc that stores the document details and it worked, but when I exited that vc, the cell deleted and the tableview shrank, basically glitching out the app, and eventually crashed when I clicked another cell with different details.
So i want to be able to have the timer be set when the document is created in one vc, and have the timer fire in another vc when the deletion date arrives.