Delete data from collection in FireStore and add to another collection at the same time

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:

  1. names of teachers
  2. teachers who are at work
  3. 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 :slight_smile:
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).

Found a solution. I needed to capture the data from collection 2 and I did it that way:

func removeAddTeacher(at indexSet: IndexSet) {
        let databaseReference = Firestore.firestore().collection("WorkingTeacherModel")
        let workingTeachers = workingTeacherViewModel.workingTeachers
        var teacher: String?
        var startTime: String?
        var startDate: String?
        indexSet.forEach { index in
            let workingTeacher = workingTeachers[index]
            teacher = workingTeacher.name
            startTime = workingTeacher.startTime
            startDate = workingTeacher.startDate
            databaseReference.document(workingTeacher.id ?? "").delete { error in
                if let error = error {
                    print("\(error.localizedDescription)")
                } else {
                    print("Working teacher with ID \(workingTeacher.id ?? "") deleted")
                }
            }
        }
        savedTeacherViewModel.addData(name: teacher ?? "" , startTime: startTime ?? "", startDate: startDate ?? "", endTime: endTime, endDate: endDate)
    }

This topic was automatically closed after 166 days. New replies are no longer allowed.