2017-08-20 37 views
1

我一直在嘗試從帖子中刪除帖子,當帖子獲取2個以上的標誌時。我嘗試了很多方法,但無法弄清楚。我有一些圖片附加幫助將不勝感激! '在標記帖子時從firebase中刪除孩子

// 4 
     if poster.uid != User.current.uid { 
      let flagAction = UIAlertAction(title: "Report as Inappropriate", style: .default) { _ in 
       PostService.flag(post) 

      /// addedflag 

      let flaggedPostRef = Database.database().reference().child("flaggedPosts").child(postKey!) 


      // 3 
      var flaggedDict = ["text": post.textData, 
           "poster_uid": post.poster.uid, 
           "reporter_uid": User.current.uid] 

      // 4 
      flaggedPostRef.updateChildValues(flaggedDict) 

      // 5 
      let flagCountRef = flaggedPostRef.child("flag_count") 
      flagCountRef.runTransactionBlock({ (mutableData) -> TransactionResult in 


       let currentCount = mutableData.value as? Int ?? 0 
       mutableData.value = currentCount 

      if mutableData.value as! Int >= 1 { 



       let uid = FIRAuth.auth()!.poster.uid!.uid 

       // Remove the post from the DB 
       ref.child("posts").child(postKey).removeValue { error in 
        if error != nil { 
         print("error \(error)") 
        } 
       } 
       postKey?.removeVolue() 
       let timelinePostDict = ["poster_uid" : poster.uid] 
       var updatedData: [String : Any] = ["timeline/\(poster.uid)/\(postKey)" : timelinePostDict] 

       let postToBeDeleted = Database.database().reference().child("posts") 
       updatedData["timeline/\(poster.uid)/\(postKey)"] = timelinePostDict 
       updatedData["posts/\(poster.uid)/\(postKey)"] = postKey 


       print("Delete case: mutableData.value = \(mutableData.value)") 

      } else { 
       print("Case not met. Either not equal to 2 or not able to cast as Integer type. The value of the casted in is \(mutableData.value as? Int)") 
      } 




       mutableData.value = currentCount + 1 


       return TransactionResult.success(withValue: mutableData) 
      }) 

`the code for flagingFriebase

+0

擴展flaging的代碼在這裏,我無法發佈它。 –

+0

這是一個有趣的問題....我最近一直在使用firebase,這是一個很好的問題!讓我們來看看,在我想了解一下之後,我是否可以在這裏向你提出一個想法。 – archae0pteryx

+0

你的標誌位置在firebase的哪裏,顯示圖像也是如何將標誌保存在firebase中? – 3stud1ant3

回答

0

.runTransactionBlock({..前的值被附加/在特定節點處更新被觸發多次。你需要的是一個觀察者: -

Database.database().reference().child("flaggedPosts").observe(.childChanged, with: {(Snapshot) in 

     // Every time a child dictionary in the flaggedPosts node 
     // would change you will receive that dictionary in this block 
     // be it any change in any of the values in that postKey 
     // Check the flagCount value and perform the action requisite... 

     print(Snapshot.value ?? "No value retrieved") 



     if let snapDict = Snapshot.value as? NSDictionary{ 

      let count = snapDict["count"] as! Int 
      let postID = snapDict["postKey"] as! String 
      let posterUID = snapDict["poster_uid"] as! String 

      if count > 1{ 

       print(postID) 

       // You will only be pushed into this block only if your count value 
       // is greater than 1 i.e 2 

       // Delete postID in your database 

       Database.database().reference().child("posts/\(posterUID)/\(postID)").removeValue(completionBlock: {(Err, ref) in 

        // Handle the error you recieve while deleting 
        // the flagged post 


       }) 
      } 
     } 

    }, withCancel: {(Error) in 

     // Handle the error recieved while making 
     // a call to firebase 

    }) 

考慮,此方法使用一個.observe(.childChanged..,這是一個異步主動呼叫,其觀察者不是從網絡鏈接中刪除。你的數據庫,這將被觸發每次你的孩子被改變/更新時間......

+0

謝謝,我只是用類似的方法修復。謝謝你 –

0
   let flaggedPostRef = Database.database().reference().child("flaggedPosts").child(postKey!) 

我定了,我只是找到了一種方法找到德海報uid和崗位關鍵,只是挪移volue