I am working on the user profile screen for one of my apps. I wanted to allow the user to select an image from his phone and be able to see his profile image in the main screen of the application. The problem I have is that an image takes longer to save than plain text. I would like to find a way to make sure the image is saved to parse before the segue takes the user to the main screen. Right now as soon as I click next, the segue takes the user to the main screen. In the main screen the user's profile image is not able to display because the profile image that was "saved" to parse is empty. When this happens i get a nil value exception.
Note: When i remove the Segue - the application successfully saves the user's info and the user's image.
the following code is after I do all my checks on username, password etc.
if photoSelected == false {
error = "Please select an image to post"
}
if error != "" {
displayAlert("Cannot Post Image", error: error)
} else {
var post = PFObject(className: "Images")
post["username"] = PFUser.currentUser().username
post.saveInBackgroundWithBlock({ (success: Bool, error: NSError!) -> Void in
if success == false {
self.displayAlert("Could not post Image", error: "Please try again later")
} else {
let imageData = UIImagePNGRepresentation(self.profileImage.image)
let profilePic = PFFile(name: "image.png", data: imageData)
post["profileImage"] = profilePic;
//I even added an activity indicator to stall the app before proceeding to the main page.
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 200, 200))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
self.view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
post.saveInBackgroundWithBlock({ (success: Bool, error: NSError!) -> Void in
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if success == false {
self.displayAlert("Could not post Image", error: "Please try again later")
} else {
//check
println("picture was uploaded")
}
})
}
})
}
//saves all the user's values username, password, etc.
self.user.save()
//takes the user to the MainScreen
self.performSegueWithIdentifier("moveToMainScreen", sender: self)
}
Thank you so much for your help!
Move these lines:
//takes the user to the MainScreen
self.performSegueWithIdentifier("moveToMainScreen", sender: self)
higher up, into the completion block for the image save (where you have println("picture was uploaded")
now.