retrieving images from Parse and showing them on a tableView, if I scroll the tableView down, app crashes and gives me "fatal error: Array index out of range"
numbers of rows is given by the number of messages in "messagesArray"
I think the problem is that in the query, if I don't find images in pointers, I say "append to the PFFile
array this standard UIImage
of mine called "logo" " so when I scroll down, count of imagesArray and messagesArray doesn't mach.
how can I add UIImages to my PFFile array so that if query for a PFFIle images fails, it can be replaced by appending a UIImage?
some told me to make a separate query for the image it in dispatch_async(dispatch_get_main_queue()) {}
but the main query is called itself that way.
I have this array:
var picturesArray : [PFFile] = []
in cellForRowAtIndexPath
I have:
self.picturesArray[indexPath.row].getDataInBackgroundWithBlock { (imageData: NSData?, error:NSError?) -> Void in
if error == nil {
let image = UIImage(data: imageData!)
cell.senderProfileImage?.image = image
} else {
print("plan B")
cell.senderProfileImage?.image = UIImage(named: "logo")
}
}
in my query, I can retrive the names of my senders by querying a "sender" column with pointers to _Users
. I have:
if let theName = singleObject.objectForKey("sender")?.objectForKey("first_name") as? String {
// this retrieves names from pointer "sender" in "Messages"
self.sendersArray.append(theName) //populate the array with names
} else {
if let messageSender = singleObject["senderNickname"] as? String {
self.sendersArray.append(messageSender)
}
}
if let profilePicture = singleObject.objectForKey("sender")?.objectForKey("profile_picture") as? PFFile {
self.picturesArray.append(profilePicture)
} else {
//I think this is the problem:
print("no image found in pointer to users")
self.picturesArray.append(UIImage(named: "logo"))
}
Solution:
for now, is also required fix this bug (hoping Apple and Parse will take care of them sooner or later)
//very new, it's ok
if let theName = singleObject.objectForKey("sender")?.objectForKey("first_name") as? String {
// this retrieves names from pointer "sender" in "Messages"
self.sendersArray.append(theName) //populate the array with names
} else {
if let messageSender = singleObject["senderNickname"] as? String {
self.sendersArray.append(messageSender)
}
}
//very new : this fix fatal error: Array index out of range
if let profilePicture = singleObject.objectForKey("sender")?.objectForKey("profile_picture") as? PFFile {
self.picturesArray.append(profilePicture)
} else {
//I think this is the problem:
print("no image found in pointer to users")
// self.picturesArray.append(UIImage(named: "logo"))
let imageData:NSData = UIImagePNGRepresentation(UIImage(named: "logo")!)!
self.picturesArray.append(PFFile(data: imageData))
}
and
//this fixes the crash related to different number of rows and images
if indexPath.row < picturesArray.count {
self.picturesArray[indexPath.row].getDataInBackgroundWithBlock { (imageData: NSData?, error:NSError?) -> Void in
if error == nil {
let image = UIImage(data: imageData!)
cell.senderProfileImage?.image = image
}
}
} else {
// cell.senderProfileImage?.image = UIImage(named: "logo")
print("no foto!")
}