Q:

Check if image exist in Parse.com

How can i check if a image exist in parse?
This is my query code:
func queryFromParse(){
        self.imageFiles.removeAll()
        var query = PFQuery(className: "currentUploads")
        query.orderByDescending("createdAt")
        query.findObjectsInBackgroundWithBlock { (posts: [AnyObject]?, error: NSError?) -> Void in
            if (error == nil){
                // Success fetching objects
                for post in posts!{
                    println(post)
                    self.imageFiles.append(post["imageFile"] as! PFFile)
                    self.imageText.append(post["imageText"] as! String)
                }
                self.collectionView.reloadData()
                println(self.imageFiles.count)

            }
            else{
                println(error)
            }
        }
    }

If a image fails to upload to parse, it looks like this:
Image Here
The application crashes if the does not contains a image, so how can i check if the image exist and skip it, if it does not include a image?

A:

I think your code is crashing because you're using a force casting in the block (post["imageFile"] as! PFFile and post["imageText"] as! String). But there is also another problem in your code. self.collectionView.reloadData() is a ui code so it needs to be done on the main queue. The findObjectsInBackgroundWithBlock method runs the block in a different thread so the closure might be executed some time later after the function has already returned. You use dispatch_async to get the main queue if you have a UI related stuff to be done in the closure. 
if let unwrappedPosts = posts as? [PFObject] {
     for post in unwrappedPosts{
         println(post)
         if let file = post["imageFile"] as? PFFile {self.imageFiles.append(file) }
         if let text = post["imageText"] as? String { self.imageText.append(text) } //the above to if lets are to be extremely safe (probably not necessary)
     }
     dispatch_async(dispatch_get_main_queue(), {
         self.collectionView.reloadData()
         print(self.imageFiles.count)        
     })
} else { print("found nil") }

