John Doe
John Doe

Reputation: 1609

Async With Parse

I'm building an app based around pictures. So it's very important for me to retrieve all images asyncronously like Instagram does. I understand that this function...

    var query = PFQuery(className: "Post")
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if let objects = objects as! [PFObjects] {
            for object in objects {
                objectsArray.append(object)
            }
        }
    }

...is asynchronous. But I want a way to load images from Parse into a table asynchronously so it loads images while scrolling.

Upvotes: 0

Views: 41

Answers (1)

Kalzem
Kalzem

Reputation: 7492

You should take a look at PFImageView's function loadInBackground().

For example, if you are using a PFTableViewController with a PFTableViewCell, you can do the following

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell!
    if cell == nil {
        cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
    }

    if let name = object?["name"] as? String {
        cell.nameLbl.text = name
    }

    var initialThumbnail = UIImage(named: "placeholder")
    cell.imgView.image = initialThumbnail
    if let thumbnail = object?["photo"] as? PFFile {
        cell.imgView.file = thumbnail
        cell.imgView.loadInBackground()
    }

    return cell
}

with PFTableViewCell having

class CustomCell: PFTableViewCell {
    @IBOutlet weak var nameLbl: UILabel!
    @IBOutlet weak var imgView: PFImageView!
}

Also from another SO reply, you can try this:

let userImageFile = userPhoto["imageFile"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
    (imageData: NSData!, error: NSError!) -> Void in
    if !error {
        let image = UIImage(data:imageData)
    }
}

Upvotes: 2

Related Questions