Hi,
I’m trying to make an app for my school. The purpose of the app is a login and logout registration for teachers. When teachers start teaching, there name and startTime is recorded. When they finish their lesson, name and endTime is recorded.
So I have 3 collections in Firestore:
- names of teachers
- teachers who are at work
- a list that registers arrivals and departures.
I can append the teachers from collection 1 to collection 2 (with startTime) but in collection 2 on FireStore I want to delete the data from a row (in a ForEach) but at the same time I want to add that data (name + startTime & endTime) to collection 3 on FireStore. I can delete the data with
.onDelete but can’t append it at the same time to the other collection. Is there a solution for this?
Thanks in advance for helping me out
Brecht
I use this function and code to add the Teacher to collection 2:
func addData(name: String, startTime: String) {
do {
_ = databaseReference.addDocument(data: ["name": name, "startTime": startTime])
}
}
collection 2: self.workingTeacherViewModel.addData(name: teacher.name ?? "", startTime: startTime)
I use this function to delete the data from collection 2:
func deleteData(at indexSet: IndexSet) {
indexSet.forEach { index in
let workingTeacher = workingTeachers[index]
databaseReference.document(workingTeacher.id ?? "").delete { error in
if let error = error {
print("\(error.localizedDescription)")
} else {
print("Working teacher with ID \(workingTeacher.id ?? "") deleted")
}
}
}
}
From collection 2: .onDelete(perform: self.workingTeacherViewModel.deleteData(at:))
Here I want to simultaniously add the data from collection 2 to collection 3 (but with an extra endDate).