I have a small issue in that after selecting an image from UIImagePickerController (and saving to Parse because my UICollectionView populates from my images on Parse), my UICollectionView will not load the newly saved image until I leave that viewController and go back to it. I've tried just about every work around that I can think of. Where should I be putting my code to save and dismiss controller?
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
let imagePicture = UIImageJPEGRepresentation(image, 0.1)
let imageFile = PFFile(name: "imageFile.jpg", data: image)
let saveImage = PFObject(className: "Gallery")
saveImage["imageFile"] = imageFile
saveImage["dogName"] = self.dogSelectedName
saveImage["userID"] = PFUser.currentUser()?.objectId
saveImage["dogID"] = self.dogObjectID
saveImage.saveInBackgroundWithBlock { (Bool, error) -> Void in
if error != nil {
print("There was an error")
} else {
print("New Gallery object saved: \(saveImage)")
}
}
self.collectionView.reloadItemsAtIndexPaths(self.collectionView.indexPathsForVisibleItems())
self.collectionView.reloadData()
self.dismissViewControllerAnimated(true) { () -> Void in}
}
After debugging, I notice a lot of the time it saves my object after the view appears (or depending where I write the code, before the view appears) and still will not reload the collection view until I leave and come back. I've put:
dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.collectionView.reloadData()
}
In multiple places in my code but still same result.. How else can/should I be setting this up? Any help is lots of help!! Thanks.