1

I am facing an issue when trying to delete objects from Parse after having queried them.

My code:

        var query = PFQuery(className:"sendMessage")
        query.whereKey("messageSent", equalTo: PFUser.currentUser()!.username!)
        query.whereKey("messageReceived", equalTo: self.nameLabel!.text!)
        query.findObjectsInBackgroundWithBlock({ (objects, NSError) -> Void in

            if  objects != nil {

                if let objects = objects as? [PFObject] {
                    for object in objects {


                        print(object["message"])

                   /// here I would go: object.deleteInBackground()
                                        object.save()

                      }
                 }
               }
         })

But it seems that I cannot find the right way to do so. Any insights ?

2
  • And what is the problem exactly? Commented Jul 19, 2015 at 18:53
  • It doesnt work.. I have an error saying "PFObject doesnt have a member named deleteInBackground" Commented Jul 19, 2015 at 18:57

2 Answers 2

1
var query = PFQuery(className:"sendMessage")
let username = PFUser.currentUser()?.username
    query.whereKey("messageSent", equalTo: username)
    query.whereKey("messageReceived", equalTo: self.nameLabel!.text!)
    query.findObjectsInBackgroundWithBlock({ (objects:[AnyObject]?, error:NSError) -> Void in
        if  error == nil {
            if let objects = objects as? [PFObject] {
                for object in objects {
                 let deletemessage = object["message"] as! String
                    print(deletemessage)
                    object.delete()
                  }
             }
           } 
            else {
                    println("Error")
               }
     })
Sign up to request clarification or add additional context in comments.

2 Comments

totally, tried checking the green sign too but it takes off the first answer that was given. Enlighten me on how to do so please.
It is unfortunate. I ll have to go with the first one given as both perfectly respond to my question. Sorry about that. I ll make sure to encounter a similar problem and ring you straight next ;) Thanks again Ravi.
0

I have used deleteEventually() with success before, together with PFObject(withoutDataWithClassName: YourClassName, objectId: YourObjectID).

If that works I wouldn't know why, but well :)

(as stated by Hector in this Parse Question (Objective-C): https://www.parse.com/questions/delete-row)

for object in objects {
    print(object["message"]
    var toDelete = PFObject(withoutDataWithClassName: "sendMessage", objectId: object.objectID)
    toDelete.deleteEventually()
} 

5 Comments

Thank you for the response. I did see that indeed, however, I would rather have to skip finding out the objects' ID before deleting.
I'm pretty sure you can access the objectId without performing another Query. Since you retrieve the objects as an Array of PFObjects the objectId should be accessible as I did above (added example).
Would go with that. Thanks. Will update you afterwards.
you got it! Didnt work with "deleteEventually()", go figure; though worked like a charm with "delete()" using your method. Thanks a lot!
Just keep in mind "delete()" is a synchronous method, and might freeze your app for long moment if the network connection is bad. Glad it worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.