Search code examples
parse-platformparse-serverswift5

Syntax for PFObject saveInBackground progressBlock


What is the Swift5 Syntax for Parse PFObject saveInBackground progressBlock? I get error "Incorrect argument labels in call (have _:progressBlock, expected withTarget:selector). The Parse documentation appears not up to date. Thanks in advance for any advice.

let imageData = image.pngData()!
let imageFileObject = PFFileObject(name: "image.png", data: imageData)
let userPhoto = PFObject(className: "ARReferenceImages")
userPhoto["imageName"] = "Test 1"
userPhoto["imageFileObject"] = imageFileObject

userPhoto.saveInBackground ({ (success: Bool, error: Error?) in       // Xcode error here
        if (success) {
            print("image saved to cloud")
        } else if error != nil {
            print("error saving data to cloud")
        }
    }, progressBlock: { (percentDone: Int32) in
        // code to update progress bar spinner here
    })

Solution

  • PFObject does not have progressBlock. PFFile is the one that has. You should use it like this:

    let imageData = image.pngData()!
    let imageFileObject = PFFileObject(name: "image.png", data: imageData)
    imageFileObject.saveInBackground ({ (success: Bool, error: Error?) in       // Xcode error here
        if (success) {
            let userPhoto = PFObject(className: "ARReferenceImages")
            userPhoto["imageName"] = "Test 1"
            userPhoto["imageFileObject"] = imageFileObject
            userPhoto.saveInBackground { (succeeded, error)  in
                if (succeeded) {
                    // The object has been saved.
                } else {
                    // There was a problem, check error.description
                }
            }
        } else if error != nil {
                print("error saving data to cloud")
        }
    }, progressBlock: { (percentDone: Int32) in
        // code to update progress bar spinner here
    })