firebaseデータベースからオブジェクトを削除する機能を持っていますが、tableViewセルにはfirebaseストレージに保存されている画像が含まれていますが、画像を削除したいデータがデータベースから削除されたときにも、ストレージから、どのようにこれを行うことができますか?削除するためのfirebase storageから画像を消去する
コード:ロード用
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
編集:
私はそれはないと思う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
}
}
:
food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!, downloadURL: downloadURL!))
をそして、これは私が削除するために使用しようとしているものです:firebaseからオブジェクトをロードするとき、私は配列にURLを追加した10
しかし、画像を見つける...
エラードメイン= 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 = {オブジェクト= https:/firebasestorage.googleapis.com/v0/b/recipe-app-1b76e.appspot.com/o/B74F604B-68FD-45BB-ABDB-150B03E83A2A.png?alt = media & token = ae2643c4-エラーメッセージ: "code":404、 "メッセージ": "見つかりません。オブジェクトを削除できませんでした"、バケット= recipe-app-1b76e.appspot.com、bucket = recipe-app-1b76e.appspot.com } }、
を[Firebaseは、ドキュメントを削除](https://firebase.google.com/docs/storage/ios/delete-files) たぶん回避策は、あなたの食べ物のオブジェクトは、ダウンロードURLを追加および削除しますstorageRef。 let desertRef = storageRef.child(food.downloadUrl) desertRef.delete ... –
@EmreYILMAZちょっと私の質問の末尾にある編集を見てください。 – random1234
@EmreYILMAZ Nevermind私はそれを修正しました!しかし、私の他の質問をチェックしてください! https://stackoverflow.com/q/46549312/7109256 – random1234