iosswiftparse-platformpfobject

Delete a row from a class in Parse - In Swift


I'm trying to make some voting buttons for my film app.

I use Parse, and I've created a new class called Votes_Up. In this class their are 2 columns: User_Voting and Film_VotedFor.

My code below is actioned when the button is pressed. It takes the currentUser and the film object that the button has been pressed against, and adds them to Parse. My database looks like this:

enter image description here

What I need

so they way it currently works, if the user presses the vote button, it adds the info to the DB and adds 1 to the count. When the user presses it again, it just updates the count by taking 1 away.

I would also want to remove the row it would have added to the database, By the User that voted for it. How can I do this??

My code:

@IBAction func upVoteButton(sender: UIButton) {

    let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
    let object = objectAtIndexPath(hitIndex)

    if PFUser.currentUser() != nil {

        if userPressedUpButton == false {

            userPressedUpButton = true

            let voteUp = PFObject(className: "Votes_Up")
            voteUp["User_Voting"] = PFUser.currentUser()
            voteUp["Film_VotedFor"] = object!

            object!.incrementKey("UpVoteCount")
            object!.saveInBackground()

            voteUp.saveInBackgroundWithBlock({ (success, error) in

                if success == true {

                    // Object(s) was/were successfully saved

                } else if error != nil {

                    // Display an alert to the user

                } else {

                    // Display an alert to the user - something went wrong

                }

            })

        } else {

            userPressedUpButton = false

           // I need to removed the row from the class here that was added, as the user has retracted their vote!

            object!.incrementKey("UpVoteCount", byAmount: -1)
            object!.saveInBackground()

        }

    } else {

        // No user is logged in so they can't vote
        performSegueWithIdentifier("Votes_LoginPopup", sender: self)

    }

    self.tableView.reloadData()

}

Solution

  • Try this:

    var query = PFQuery(className:"Votes_Up")
        query.whereKey("User_Voting", equalTo:PFUser.currentUser())
        query.limit = 1
        query.findObjectsInBackgroundWithBlock {
            (votesUp: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
                if let objects = votesUp as? [PFObject] {
                    var firstObject = objects[0]
                    dispatch_async(dispatch_get_main_queue(),{
                        // do delete here
                        firstObject.deleteEventually();
                    })
    
                }
            }
        }
    

    or this:

    let query = PFQuery(className: "Votes_Up")
    query.whereKey("User_Voting", equalTo: PFUser.currentUser())
    query.findObjectsInBackgroundWithBlock {
    (objects: [AnyObject]?, error: NSError?) -> Void in
        for object in objects {
            object.deleteEventually()
        }
    }