I am following the tutorial on creating an instragram-esque application, and I have a lot of problems when I figure out how to delete a message from both Firebase and the feed. The image that the user selects or accepts is uploaded to the Firebase database using this function:
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() }
The end in Firebase looks like this:

After the response to the stack overflow that I found, I tried to configure the delete function, which will be called when the delete button is clicked. This delete button is located on the "photo details" view that the user receives by clicking on the image in the image feed - this detailed photo view displays a larger image, as well as other information such as:
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)") } } }
And call the function here:
@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) }
I donβt quite understand what Iβm doing, and, of course, pressing the delete button means almost nothing. Can someone show me how to fix the delete function so that I can correctly remove the image / message from firebase?
EDIT: I have var selectedPost: Post! in my PhotoDetailController, which is passed from the FeedViewController (image feed) to didSelectItem as follows:
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) }
So it has an index path. Another note about the above function is that var posts = [Post]() is created in FeedViewController, so where did posts[indexPath.row] .