Search code examples
swiftparse-server

Deleting object from parse-server code causing error?


I am developing an app in Xcode using swift and am using a heroku-hosted parse-server as my database. I want to be able to delete an object from the database, but I keep getting an error when trying to type out the code. Here is what I have:

{
        let removingObjectQuery = PFQuery(className: "GoingTo")
        removingObjectQuery.whereKey("objectId", equalTo: goingToSelectionID)
        removingObjectQuery.findObjectsInBackground(block: { (object, error) in
            if let objects = object{
                print("Object found")
                for object in objects{
                        object.deleteInBackground()  
                }
            }
        })
    }

But the delete .deleteInBackground keeps sending an error to in the code line saying that ".deleteInBackground is not a member of [PFObject]"... except that I thought it is a member of that value type?

Edit: Syntax fixed to allow calling of .deleteInBackground but now am receiving error in logs (which does not crash the app) that "[Error]: Object not found". The object is for sure in the DB and whereKey equalTo: is adequately described... (goingToSelectionID is indeed the objectId in the DB... checked this by printing to logs). Not sure what is wrong?


Solution

  • The findObjectsInBackground method doesn't return results of type PFObject, but [PFObject], which is an array of PFObjects... If you want to delete the whole array, you can use the class method deleteAllInBackground like so:

    PFObject.deleteAllInBackground(objectt, block: nil)
    

    Or you can iterate through the array:

    for objectt in object! {
        objectt.deleteInBackground()
    }