我一直在关注制作一个instragram-esque应用程序的教程,我在查找如何从Firebase和Feed中删除帖子时遇到了很多麻烦。使用此功能将用户选择或拍摄的图像上传到Firebase数据库:
func uploadToFirebase() {
AppDelegate.instance().showActivityIndicator()
let uid = FIRAuth.auth()!.currentUser!.uid
let ref = FIRDatabase.database().reference()
let storage = FIRStorage.storage().reference(forURL: "gs://cloudcamerattt.appspot.com")
let key = ref.child("posts").childByAutoId().key
let imageRef = storage.child("posts").child(uid).child("\(key).jpg")
let data = UIImageJPEGRepresentation(self.previewImage.image!, 0.6)
let uploadTask = imageRef.put(data!, metadata: nil) { (metadata, error) in
if error != nil {
print(error!.localizedDescription)
AppDelegate.instance().dismissActivityIndicator()
return
}
imageRef.downloadURL(completion: { (url, error) in
if let url = url {
// how do I add date: NSDate in here?
let feed = ["userID" : uid,
"pathToImage" : url.absoluteString,
"likes" : 0,
"author" : FIRAuth.auth()!.currentUser!.displayName!,
"postID" : key] as [String : Any]
let postFeed = ["\(key)" : feed]
ref.child("posts").updateChildValues(postFeed)
AppDelegate.instance().dismissActivityIndicator()
self.dismiss(animated: true, completion: nil)
}
})
}
uploadTask.resume()
}
最终Firebase中的内容如下所示:
在我发现堆栈溢出回答后,我尝试设置一个删除按钮时按下删除按钮的功能。此删除按钮位于“照片详细信息”视图中,用户可通过点击图像Feed中的图像来访问该视图 - 此照片详细信息视图以更大的尺寸显示图像,以及其他一些信息,例如:
func deletePost(firstTree: String, childIWantToRemove: String) {
let uid = FIRAuth.auth()!.currentUser!.uid
let ref = FIRDatabase.database().reference()
let storage = FIRStorage.storage().reference(forURL: "gs://cloudcamerattt.appspot.com")
let key = ref.child("posts").childByAutoId().key
let imageRef = storage.child("posts").child(uid).child("\(key).jpg")
ref.child("posts").child(key).child("postID").removeValue { (error, ref) in
if error != nil {
print("error \(error)")
}
}
}
在这里调用函数:
@IBAction func moreButtonPressed(_ sender: AnyObject) {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
let destroyAction = UIAlertAction(title: "Delete", style: .destructive) { action in
print(action)
let ref = FIRDatabase.database().reference()
let key = ref.child("posts").childByAutoId().key
let firstTree = key
let valueToRemove = "postID"
self.deletePost(firstTree: firstTree, childIWantToRemove: valueToRemove)
}
alertController.addAction(destroyAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true)
}
我并不是真正理解我在做什么,不用说点击删除按钮基本上什么也没做。任何人都可以告诉我如何修复删除功能,以便我可以正确删除firebase中的图像/帖子吗?
编辑:我的PhotoDetailController中有var selectedPost: Post!
,它从didSelectItem
中的FeedViewController(图片Feed)传递,如下所示:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let photoDetailController = self.storyboard?.instantiateViewController(withIdentifier: "photoDetail") as! PhotoDetailController
photoDetailController.selectedPost = posts[indexPath.row]
present(photoDetailController, animated: true, completion: nil)
}
所以它有索引路径。关于上述函数的另一个注意事项是var posts = [Post]()
在FeedViewController中实例化,因此posts[indexPath.row]
来自{。}}。
答案 0 :(得分:7)
这应该有用。
func deletePost() {
let uid = FIRAuth.auth()!.currentUser!.uid
let storage = FIRStorage.storage().reference(forURL: "gs://cloudcamerattt.appspot.com")
// Remove the post from the DB
ref.child("posts").child(selectedPost.postID).removeValue { error in
if error != nil {
print("error \(error)")
}
}
// Remove the image from storage
let imageRef = storage.child("posts").child(uid).child("\(selectedPost.postID).jpg")
imageRef.delete { error in
if let error = error {
// Uh-oh, an error occurred!
} else {
// File deleted successfully
}
}
}
此外,.childByAutoId().key
会生成一个将项目插入数据库的密钥。您无法使用它获取对现有项目的引用。
答案 1 :(得分:1)
根据Firebase文档,您也可以通过
删除它将null指定为另一个写操作的值,例如set() 或更新()
您可以将此技术与update()一起使用,以在单个API调用中删除多个子项。