Allowing specefic users to delete post Swift 4 Fir

2019-03-06 10:34发布

i have just created so the users can comment on other users post and delete the comments if they want to delete them.

but with this code everyone can delete everyone´s comments

func getKeysValue() {

   // let uid = Auth.auth().currentUser?.uid

    Database.database().reference().child("posts").child(postsKey).child("comments").observe( .value) { (snapshot) in
        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let commentKey = snap.key
            self.keyArray.insert(commentKey, at: 0)


         //   print(self.keyArray)
           // print("Here is the specific Key\(self.commentKey)")
        }
    }

}


   func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {



    if editingStyle == .delete {
       getKeysValue()

        let when = DispatchTime.now() + 1
        DispatchQueue.main.asyncAfter(deadline: when, execute: {

          Database.database().reference().child("posts").child(postsKey).child("comments").child(self.keyArray[indexPath.row]).removeValue()


                        self.commentsTableView.reloadData()


                        })
                }
            }

then i tryed this code to only let the user to only delete its own comments:

 func getKeysValue() {

   // let uid = Auth.auth().currentUser?.uid

    Database.database().reference().child("posts").child(postsKey).child("comments").observe( .value) { (snapshot) in
        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let commentKey = snap.key
            self.keyArray.insert(commentKey, at: 0)


         //   print(self.keyArray)
           // print("Here is the specific Key\(self.commentKey)")
        }
    }

}


var uid = Auth.auth().currentUser?.uid
var userID = String()
var idUser = String()
var userUID = String()

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {



    if editingStyle == .delete {
       getKeysValue()

        let when = DispatchTime.now() + 1
        DispatchQueue.main.asyncAfter(deadline: when, execute: {

            self.userID = self.keyArray[indexPath.row]

            self.idUser = self.userID
           print(" Here is the indexPath  key: \(self.idUser)")
            Database.database().reference().child("posts").child(postsKey).child("comments").child(self.idUser).observeSingleEvent(of: .value, with: { (snapshot) in


                if let postsDictionary = snapshot.value as? [String: AnyObject] {
                    self.uidArray.insert(postsDictionary as NSDictionary, at: 0)


                    let usersArray = self.uidArray[indexPath.row] as! [String: AnyObject]
                    let usersKey = usersArray["uid"] as? String
                    self.userUID = usersKey!


                            print("Here is the Comment:\(self.uidArray)")
                            print("And here is the UID::: \(self.userUID)")

                    if self.uid != self.userUID {

                        let logInAlert = UIAlertController(title: "Failed to delete", message: "You can only delete your own comment(s)", preferredStyle: .actionSheet)
                        logInAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                        self.present(logInAlert, animated: true, completion: nil)

                    }else {

                        let commentSuccess = self.storyboard?.instantiateViewController(withIdentifier: "CommentReloadVC")
                        self.present(commentSuccess!, animated: false, completion: nil)

                        Database.database().reference().child("posts").child(postsKey).child("comments").child(self.keyArray[indexPath.row]).removeValue()


                        self.commentsTableView.reloadData()

                                }
                            }
                        })
                    })
                }
            }

soo whats wrong with this code? the thing is that this work perfect but only on the first row off the tableview that will say that this authorazation code only works with the first comment of the post and if you try to delete any other comment below the first one then i get an error that the index is out of range on this line

let usersArray = self.uidArray[indexPath.row] as! [String: AnyObject]

and this is how it looks in the database: enter image description here

this looks like it should work but it does not it's probably something really easy but i just cant figure this out so help would be much appreciated

Thanks for your time. :)

0条回答
登录 后发表回答