我有一个功能,可以通过滑动 tableView 单元格从我的 firebase 数据库中删除一个对象,但是,我的 tableView 单元格还包含保存在 firebase 存储中的图像,我希望图像也从存储中删除数据已从数据库中删除,我该如何完成?
删除代码:
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)
})
})
}
}
加载代码:
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()
}
}
}
})
如果有人也能帮我解决我问过的关于同一个应用程序的另一个问题,我也将非常感激:Convert observe .value to .childAdded in swift
编辑:
我在从 firebase 加载对象时将 URL 添加到数组中:
food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!, downloadURL: downloadURL!))
这就是我要用来删除的内容:
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
}
}
我认为它没有找到图像,但是...我收到此错误:
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" } },
最佳答案
解决了!这是对我有用的:
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
}
}
关于ios - Swift 从 firebase 存储中删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46561701/