2017-03-12 78 views
5

我一直在關注如何製作一個instragram-esque應用程序的教程,而且我很難找出如何從Firebase和從飼料。用戶選擇或需要將圖像上傳到火力地堡數據庫使用此功能:如何從Firebase中刪除一個孩子(Swift)

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() 
} 

其中在火力地堡最終看起來像這樣:

enter image description here

繼堆棧溢出的答案,我發現,我嘗試設置一個刪除功能,當按下刪除按鈕時被調用。此刪除按鈕位於「照片詳情」視圖中,用戶通過點按圖像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中刪除圖像/文章?

編輯:我有var selectedPost: Post!在我PhotoDetailController,這是從FeedViewController(圖像飼料)中didSelectItem通過像這樣:

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

您在detailView中有詳細視圖中當前帖子的信息的帖子對象嗎?或者包含帖子信息的任何對象(如ID)? –

+0

Hey Pieter yes我有一個變量,它包含Post對象和從Feed中選擇的圖像的索引路徑 - 檢查我的編輯,我將代碼放在那裏以使其更清晰。 – KingTim

回答

4

這應該工作。

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生成一個鍵將項插入到數據庫中。您無法使用它獲取對現有項目的引用。

+0

非常感謝!它工作完美。看着你的代碼,這實際上最終比我想象的更簡單,我試圖弄清楚如何使用childByAutoID。再次感謝非常感謝! – KingTim