iosswiftparse-platformback4app

Swift: Update Parse User (PFUser) and save image to it after new registration


I have been banging my head since yesterday. I have fetch profile picture from facebook and want to save it to parse. But I am stuck. I tried solution from this

How to update Parse user information in Swift?

That did not solved my case.

Updating user in Parse.com using Swift IOS8

I dont think this is the case, since I have just logged in. and my view changes too.

here's my code.

func updateCurrentUserProfilePicture(image: UIImage) {
        let currentUser = PFUser.currentUser()
        let id = currentUser?.objectId
        let data = UIImagePNGRepresentation(image)
        var query = PFUser.query()

        query!.getObjectInBackgroundWithId(id!) {
            (user: PFObject?, error: NSError?) -> Void in
            if error != nil {
                print(error)
            } else if let usr = user {
                // usr.setObject(data!, forKey: "image")
                usr["image"] = data!
                usr.saveInBackgroundWithBlock({ (result: Bool, error: NSError?) in
                    if error == nil {
                            self.delegate?.didUpdateProfilePictureWithResult!(true, error: error)
                    }else {
                        self.delegate?.didUpdateProfilePictureWithResult!(false, error: error)
                    }

                })
            }
        }
    }

I can see that didUpdateProfilePictureWithResult delegate is called with success. But when I go to back4app.com, I can see the user row but not the image column. And I dont see any error too.

What am I missing here?

Update I tried to save in in the console. It saved without any error.

**expression do { try usr.save()} catch { print(error)}**
2016-06-10 17:29:32.264 GeofenceMe2[39334:91037] Warning: A long-running operation is being executed on the main thread. 
 Break on warnBlockingOperationOnMainThread() to debug.
NilError

but still no image column in my dashboaard


Solution

  • You cannot save NSData to parse. Checkout the valid data types here: https://parse.com/docs/ios/guide#objects-data-types.

    You are querying for the user object using the same user object. You can just use PFUser.currentUser().

    func updateCurrentUserProfilePicture(image: UIImage) {
        let avatar = PFFile(name: PFUser.currentUser()!.username, data: UIImagePNGRepresentation(image)!)
        PFUser.currentUser()!.setObject(avatar!, forKey: "avatar")
        PFUser.currentUser()!.saveInBackgroundWithBlock { (success: Bool, error: NSError?) in
    
        }
    }