【发布时间】:2018-03-15 15:51:23
【问题描述】:
我有一个函数可以通过滑动 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
}
}
我认为它没有找到图像,但是...我收到此错误:
错误域=FIRStorageErrorDomain 代码=-13010 "对象 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 不存在。” 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={ “错误”: { “代码”:404, “消息”:“未找到。无法删除对象” } },
【问题讨论】:
-
Firebase Delete Doc 也许解决方法,您的食物对象添加下载 url 并删除 storageRef。 let DesertRef = storageRef.child(food.downloadUrl) desertRef.delete...
-
@EmreYILMAZ 嘿,请看我问题底部的编辑
标签: ios swift uitableview firebase firebase-realtime-database