Home > Enterprise >  How to delete files from Firebase Storage and Table View - Swift
How to delete files from Firebase Storage and Table View - Swift

Time:07-20

I'm creating a Customer Management app for iOS. I'm using Firebase Firestore and Storage. There is a part of my app when the user can attach document to each customer. I upload and then download the file in local folder to be able to display the file name in a TableView and open with QLPreviewController.

I save the path and name of uploaded file in Firestore.

This is my code where I want to delete the file with swipe. This work only if i try to delete the first file in table view but if try to delete the third file for example it will delete the first.

I have try this but it doesn't work for me: enter image description here

CodePudding user response:

UPDATE This is how i managed to solve my issue for deleting files in Storage the reference in Firestore and the downloaded file also i remove from the table directly.

This help me: Swift Firestore - Delete Table Cell and Remove Document from Firestore

    let localFileName = fileURLs[indexPath.row].lastPathComponent
     var storageURL = ""
     db.collection(K.FStore.customerCollectionName).document(documentListRef)
         .collection(K.FStore.docCollectionName).whereField("fileName", isEqualTo: localFileName).getDocuments{ snapshot, error in
         if let error = error {
             print(error.localizedDescription)
         } else {
             for document in snapshot!.documents {
                 //print("\(document.documentID) => \(document.data())")
                 let data = document.data()
                 if  let url = data["url"] as? String{
                     storageURL = url
                 }
        if document.documentID != "" {

         let storageRef = Storage.storage().reference()
         //Get the sotrage reference from firesore
         let fileRef = storageRef.child(storageURL)
         //Delete the specific file in firestore
         fileRef.delete { error in
             if let error = error{
                 print("Error while deleting the document \(error)")
             } else{
                 print("File deleted successfully")
                 //Delete the document from firestore
                 self.db.collection(K.FStore.customerCollectionName).document(self.documentListRef)
                     .collection(K.FStore.docCollectionName).document(document.documentID).delete(){ err in
                     if let err = err {
                         print("Error while deleting the document: \(err)")
                     } else {
                         //Get the local file path
                         let docsurl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
                         let tempDirPath = docsurl.appendingPathComponent("\(K.FStorage.documentFolderName)/\(self.documentListRef)/\(localFileName)")
                         do {
                             //Delete the local file from path
                             try FileManager.default.removeItem(at: tempDirPath)
                         } catch  { print(error) }
                         print("Document successfully deleted")
                         
                         //Remove the file from the TableView
                         self.fileURLs.remove(at: indexPath.row)
                         tableView.deleteRows(at: [indexPath], with: .fade)
  • Related