I want to delete a document from Google FireStore:
Model
import SwiftUI
import Foundation
import FirebaseFirestoreSwift
struct Agenda: Identifiable, Codable {
@DocumentID var id: String? = UUID().uuidString
var nome: String
var telefone: String
enum CodingKeys: String, CodingKey {
case id
case nome
case telefone
}
}
My Function
func excluirDaAgenda(documento: String) {
db.collection("agenda").document(documento).delete() { err in
if let err = err {
print("Error removing document: \(err)")
} else {
print("Document successfully removed!")
}
}
MyView with fields to delete:
import SwiftUI
struct ExcluirDaAgendaView: View {
var vModel : AuthViewModel
var vModel2 : AgendaViewModel
var body: some View {
ScrollView {
VStack (spacing:30) {
Text("Total de Contatos: \(vModel2.agenda.count)")
VStack (alignment:.leading, spacing:20) {
ForEach(vModel2.agenda) { contato in
HStack {
VStack {
Text(contato.nome)
Text(contato.telefone)
}
Button{
vModel2.excluirDaAgenda(documento:contato.id!)
}
label:{
Text("Excluir")
}
}
}
}.multilineTextAlignment(.leading)
}
}
.navigationTitle("Cadastro Agenda")
}
}
Hi, I don't want to create an id with UUID().uuidString, I want to create an id by retrieving the same value in the id document in the firestore database, how do I do that?