0

I'm working on a Parse query that pulls geo-based names and photos then loading them onto a table view. The first part of the query is coming through without an issue. I'm stuck on the second part of pulling/loading photos.

I could be going wrong with a missing call for the images in queryForTable method. Here I've tried includeKey (which failed as it's not a pointer to another object); I've also called for images, using getDataInBackgroundWithBlock. In each instance, I would then try loading the queried objects onto table view with a line like cell.imageData.image = photo.

But differing SO accounts also indicate that the call could go in the tableView method, which is what I have below. I've combed through SO, Parse.com and other resources, but I haven't come across a solution yet. Hoping someone can help:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject?) -> Stores! {
   let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Stores

    if let object = object {
        let Name = object.valueForKey("Name") as? String
        cell.storeList.text = Name

        let photo = object.objectForKey("Photos") as? PFFile
        photo?.getDataInBackgroundWithBlock {(imageData: NSData!, error: NSError!) -> Void in
            if error == nil {
                let image = UIImage(data: imageData)
                 cell.storeImage.file = photo
            }
        }}

    return cell as Store
Christine
  • 53
  • 12
  • Running a background block as cells dequeu is a bad idea. – ericgu Apr 27 '15 at 02:50
  • You can use my hack: http://stackoverflow.com/questions/28649257/parse-pfquerytableviewcontroller-pfquerytableviewcell-subclass-breaks-pfimag – ericgu Apr 27 '15 at 02:54
  • Thanks, Eric! I tried implementing the hack, but the image didn't come up either. I have removed "getDataInBackgroundWithBlock" though. – Christine Apr 27 '15 at 05:44
  • You'll have to debug. Mine works – ericgu Apr 27 '15 at 18:41
  • Okay, I'll definitely revisit. Thanks! – Christine Apr 27 '15 at 19:02
  • @ericgu - the block runs on the main, and is perfectly okay in cellForRowAtIndexPath. What isn't okay is referring to the cell within the block, since by the time the get completes, the cell may no longer be associated with the indexPath. Better to reloadRowsAtIndexPaths from within the block. – danh Apr 29 '15 at 01:06
  • @Christine, what sort of object is storeImage? I would expect it to be an `UIImageView`, and the property you want set is `storeImage.image` – danh Apr 29 '15 at 01:09
  • @danh Thanks for the clarification. As an object, storeImage is a PFImageView, a subclass of UIImage. When I tried setting it to .image, I got an error that states value cannot be assigned to type 'PFFile?' to a value of type 'UIImage?'... – Christine Apr 30 '15 at 00:47

2 Answers2

0

Instead of using cell.storeImage.file = photo, you should use image.

So within your completion block, you should have:

let image = UIImage(data: imageData)
cell.storeImage.file = image
BHendricks
  • 4,423
  • 6
  • 32
  • 59
  • Thanks for the suggestion. I fixed as suggested. Unfortunately, the image is still not showing. – Christine Apr 27 '15 at 01:25
  • is the completion block of the getData call being executed? – BHendricks Apr 27 '15 at 05:10
  • The call was getting executed to an extent. Testing it with breakpoints, there were no return objects indicated. – Christine Apr 27 '15 at 08:49
  • So then it's more of an issue with Parse and getting the file than it is with the image data. Have you tried using URLs to reference your images? – BHendricks Apr 27 '15 at 16:18
  • Yea, I also thought this might be a way to circumvent the issues. For a newbie, I have less confidence to judge one way or another though. So it's great to get confirmation that it's a worthwhile avenue to pursue. Thanks! I'll look into this. – Christine Apr 27 '15 at 18:57
  • Yea, there are a lot of pods too that will take a image URL and download the image in the background for you, putting a placeholder image in there while the image is downloaded and asynchronously fills the image in. This is usually the best way – BHendricks Apr 27 '15 at 19:01
  • Thanks, I'm looking into this. I've been trying to install CocoaPods for the past two days to help with it. Simple from what I've read, but not so much for when files are missing or paths have been directed. – Christine Apr 30 '15 at 00:57
0

What finally worked for me was placing the file property within the call as such:

cell.imageView.loadInBackground {(image: UIImage!, error: NSError!) -> Void in 
  if error == nil {
    cell.imageView.file = object!.objectForKey("Photos") as? PFFile
   }

A revisit to Parse docs showed that it's '.file,' not '.image'. Also, I went with 'loadInBackground', as opposed to 'loadInBackgroundWithBlock', for an asynchronous call. Hope this can help someone who's puzzled over the same issue.

Thanks to @BHendricks, @ericgu, @danh for sharing their views! Trying out all your suggestions definitely brought me steps closer to my answer.

Christine
  • 53
  • 12