0

I am doing a PFQuery on my user class to get images in background.

I have different groups so my goal is to iterate over the groups and get their specific imageData so that I can use them in my UITableView.

In my PFQuery I am using another array temp:[NSData] to store the elements in this array and later to append temp to an array of arrays groupImages:[[UIImage]].

Doing the following query:

note: member:[[AnyObject]] stores the objectId of each group.

let memberImageQuery = PFUser.query()
memberImageQuery?.whereKey("objectId", containedIn: member)
memberImageQuery?.findObjectsInBackgroundWithBlock({ (results, error) -> Void in
    if error != nil{
        // ...
    } else {
        if let results = results {
            self.temp.removeAll()
            for result in results {                
                let imageFile = result["firstImage"] as! PFFile
                imageFile.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in

                    if error != nil {      
                        print("error")
                    } else {
                        if let data = imageData {
                           self.temp.append(data)
                        }
                    }
                }

                self.groupImages.append(self.temp)
            }
        }

    self.tableView.reloadData()
}})

What happens is: temp is basically 0 though printing data in the closure gives back data. The query seems to be correct but the storing of it's data doesn't work. My idea is that it is due to badly structured code.

groupImages.count gives back the right amount of groups

temp.count gives back 0 which is not the right amount of images.

1 Answer 1

1

The image downloading is done in the background so it isn't complete by the time you append the array (at the end of the loop).

After each image download is complete you could reload the table view. You have added the array to your data source array and its mutated after each image load so your data will change multiple times but will eventually turn out correct.

Sign up to request clarification or add additional context in comments.

4 Comments

It seems to me, that this is very in efficient. is there another way to do it? Edit: tried it, but still no results.
You store the images as separate files so each requires a distinct download. You can add them to a queue instead and only update when the queue is empty. Check the array contents after each image completes
As I'm new to programming, could you quickly explain how to use queues in Swift? Would be pretty helpful, as I'm not knowing what exactly to google.
It's a concept rather than an exact implementation, you could use NSOperationQueue or simply an array that you check after each download to see if more are required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.