Search code examples
swiftparse-platformios9xcode7.3

Unable to update imagefile column in Parse using Swift


I am selecting a cell in table view, passing value to view controller and then allow user to update the row on Parse (running on Heroku) using below code.

Issue i am facing is - "title" column is updated but imagefile column doesnt get updated with new image selected. I can see that new image is indeed passed into the code.

I am using synchronous call because dont want user to move ahead unless record is saved.

what could be wrong? Same code to save a new object works fine.

Thanks Ashish

                let query = PFQuery(className:"class")
                query.whereKey("objectId", containsString: passedObject.objectId)

                do {

                    let results = try query.findObjects() 

                    if results.count == 0 {

                         print("error")

                        success = false

                     } else {
                        //update


                        do {

                            let obj = results.first

                            obj!["title"] = Title.text!

                            let imageData = UIImageJPEGRepresentation(imageOutlet.image!,0.2)
                            let imageFile = PFFile(name: "image.png", data: imageData!)


                            //try imageFile?.save() // is this needed?
                            obj!["imagefile"] = imageFile

                            try obj!.save()
                            self.success = true 


                        }  catch let er as NSError {

                            print(" error while updating - \(er)")
                        }

                     }
                } catch {

                    print("error while querying  \(error)")

                    success = false
                }

Solution

  • I ran some tests on my side and this code works for me:

    Aync Version

        let query = PFQuery(className: "FileTest")
        query.findObjectsInBackgroundWithBlock {
            (objects: [PFObject]?, error: NSError?) -> Void in
    
            let imageData = UIImageJPEGRepresentation(self.imageOutlet.image!, 0.2)
            let fileToSave = PFFile(name: "myfile.png", data: imageData!)
    
            let firstObj = objects?.first
            firstObj!["fileToSave"] = fileToSave
            firstObj!.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
                print("Object has been saved.")
            }
    
    
        }
    

    Sync Version

        let query = PFQuery(className: "FileTest")
    
        do {
            let objects = try query.findObjects()
            let firstObj = objects.first
    
            let imageData = UIImageJPEGRepresentation(self.imageOutlet.image!, 0.2)
            let fileToSave = PFFile(name: "myfile.png", data: imageData!)
            firstObj!["fileToSave2"] = fileToSave
            try firstObj!.save()
            print("object saved")
    
    
        } catch let er as NSError {
            print("error")
        }