I have a function that deletes an object from my firebase database by swiping the tableView cell, however, my tableView cells also contain images that are saved in firebase storage, I would like for the image to be deleted from the storage too when the data is deleted from the database, how can I accomplish this?
Code for deleting:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let name = food[indexPath.row].name
let ref = Database.database().reference().child("Recipes")
ref.queryOrdered(byChild: "Name").queryEqual(toValue: name).observe(.childAdded, with: { (snapshot) in
//Removes deleted cell from firebase
snapshot.ref.removeValue(completionBlock: { (error, reference) in
if error != nil {
print("There has been an error: \(error)")
}
//Removes deleted cell from array
food.remove(at: indexPath.row)
//Removes deleted cell from tableView
tableView.deleteRows(at: [indexPath], with: .left)
})
})
}
}
Code for loading:
let parentRef = Database.database().reference().child("Recipes")
let storage = Storage.storage()
parentRef.observe(.value, with: { snapshot in
if ( snapshot.value is NSNull ) {
// DATA WAS NOT FOUND
print("– – – Data was not found – – –")
} else {
//Clears array so that it does not load duplicates
food = []
// DATA WAS FOUND
for user_child in (snapshot.children) {
let user_snap = user_child as! DataSnapshot
let dict = user_snap.value as! [String: String?]
//Defines variables for labels
let recipeName = dict["Name"] as? String
let recipeDescription = dict["Description"] as? String
let downloadURL = dict["Image"] as? String
let storageRef = storage.reference(forURL: downloadURL!)
storageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) -> Void in
let recipeImage = UIImage(data: data!)
food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!))
self.tableView.reloadData()
}
}
}
})
Would also really appreciate if someone could also help me with this other question I asked about this same app: Convert observe .value to .childAdded in swift
Edit:
I have added the URL to the array when loading the objects from firebase:
food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!, downloadURL: downloadURL!))
And this is what I'm trying to use to delete:
let storage = Storage.storage()
let storageRef = storage.reference()
let desertRef = storageRef.child(food[indexPath.row].downloadURL)
//Removes image from storage
desertRef.delete { error in
if let error = error {
print(error)
} else {
// File deleted successfully
}
}
I don't think it's finding the image, however... I get this error:
Error Domain=FIRStorageErrorDomain Code=-13010 "Object https:/firebasestorage.googleapis.com/v0/b/recipe-app-1b76e.appspot.com/o/B74F604B-68FD-45BB-ABDB-150B03E83A2A.png?alt=media&token=ae2643c4-6479-4dc8-b389-d04caac98392 does not exist." UserInfo={object=https:/firebasestorage.googleapis.com/v0/b/recipe-app-1b76e.appspot.com/o/B74F604B-68FD-45BB-ABDB-150B03E83A2A.png?alt=media&token=ae2643c4-6479-4dc8-b389-d04caac98392, bucket=recipe-app-1b76e.appspot.com, ResponseBody={ "error": { "code": 404, "message": "Not Found. Could not delete object" } },
Solved it! Heres what worked for me:
let storage = Storage.storage()
let url = food[indexPath.row].downloadURL
let storageRef = storage.reference(forURL: url)
//Removes image from storage
storageRef.delete { error in
if let error = error {
print(error)
} else {
// File deleted successfully
}
}
create reference based on image location.
// Create a reference to the file to delete
let imageRef = storageRef.child("image.png")
// Delete the file
imageRef.delete { error in
if let error = error {
// Uh-oh, an error occurred!
} else {
// File deleted successfully
}
}
add the above code before following line
food.remove(at: indexPath.row)
Storing/Downloading/Deleting images or videos Using Firebase Cloud Storage!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With