viktor
viktor

Reputation: 31

Issue with deleting object from parse

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 ?

Upvotes: 1

Views: 78

Answers (2)

Lamour
Lamour

Reputation: 3030

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

Upvotes: 1

Mattias
Mattias

Reputation: 415

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

Upvotes: 0

Related Questions