我有一个功能,通过滑动tableView单元格从我的firebase数据库中删除一个对象,但我的tableView单元格还包含保存在firebase存储中的图像,我希望图像被删除从数据库中删除数据时,也从存储中读取数据,我该如何实现?从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
编辑:
加载从火力的对象时,我已经加入的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
}
}
我不认为这是然而,寻找图像...我得到这个错误:
错误域= FIRStorageErrorDomain代码= -13010“对象的https:/firebasestorage.googleapis.com/v0/b/recipe-app-1b76e.appspot.com/o /B74F604B-68FD-45BB-ABDB-150B03E83A2A.png?alt=media & token = ae2643c4-647 9-4dc8-b389-d04caac98392不存在。“ 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” } },
[火力地堡删除文件(https://firebase.google.com/docs/storage/ios/delete-files) 可能的解决方法,你的食物对象添加下载网址和删除storageRef。 让desertRef = storageRef.child(food.downloadUrl) desertRef.delete ... –
@EmreYILMAZ嘿,请看看我的问题底部的编辑 – random1234
@EmreYILMAZ Nevermind i fixed it!请检查我的另一个问题然而! https://stackoverflow.com/q/46549312/7109256 – random1234