I have a Parse database which has records consisting (among other items) of PFFile items of thumbnail images. The database is read-only and was created successfully using another program. I confirmed that all thumbnails are in Parse. When I try to retrieve the thumbnails using the function shown below, I get the notification before all the images are processed resulting on occasional failures based on the timing of the post-notification processing. How can I ensure that all records are processed?
func convertPFilesToImages () {
println("convertPFilesToImages")
let notification = NSNotification(name: "imagesLoaded", object: self)
for i in 0 ..< records.count {
let userImageFile = records[i].icon
records[i].image = UIImage()
println("name: \(self.records[i].name) image: \(self.records[i].image)")
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
//println("no error")
if let imageData = imageData {
let image = UIImage(data:imageData)!
self.records[i].image = image
// println below shows that not all images are converted
println("name: \(self.records[i].name) image: \(self.records[i].image)")
}
} else {
println("ParseProcessing-convertToImage: error = \(error)")
}
if (i == self.records.count-1) {
println("All images processed")
// loop is done before all images are processed
NSNotificationCenter.defaultCenter().postNotification(notification)
}
}
}
}