UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

FireStore Delete

Forums > SwiftUI

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?

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.