Ios 快速从firebase存储中删除图像
我有一个功能,可以通过滑动tableView单元格从firebase数据库中删除对象,但是,我的tableView单元格也包含保存在firebase存储中的图像,我希望在从数据库中删除数据时也从存储中删除图像,我如何才能做到这一点 删除代码:Ios 快速从firebase存储中删除图像,ios,swift,uitableview,firebase,firebase-realtime-database,Ios,Swift,Uitableview,Firebase,Firebase Realtime Database,我有一个功能,可以通过滑动tableView单元格从firebase数据库中删除对象,但是,我的tableView单元格也包含保存在firebase存储中的图像,我希望在从数据库中删除数据时也从存储中删除图像,我如何才能做到这一点 删除代码: func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
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()
}
}
}
})
加载代码:
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()
}
}
}
})
如果有人能帮我解决我问的关于同一个应用程序的另一个问题,我也会非常感激:
编辑:
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()
}
}
}
})
从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不存在。”用户信息={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={
“错误”:{
“代码”:404,
“消息”:“未找到。无法删除对象”
}
},
基于图像位置创建参考
// 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
}
}
在下一行之前添加上面的代码
food.remove(位于:indexPath.row)
!解决了!以下是对我有效的方法:
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
}
}
也许解决方法是,您的食物对象添加下载url并删除storageRef.let-desertRef=storageRef.child(food.downloadUrl)desertRef.delete…@EmreYILMAZ嘿,请查看我的question@EmreYILMAZ没关系,我修正了!请检查我的其他问题!嘿,请看我问题底部的编辑没关系,我修正了!请检查我的其他问题!