Cari95
Cari95

Reputation: 313

More appropriate way to retrieve multiple Parse Images

The code below works expectedly but I was wondering if I was retrieving multiple parse images the best way possible. What the code below does is retrieve 3 different PFFile columns by calling findObjectsInBackgroundWithBlock 3 separate times. Can this be condensed? If so, how can I better refine my function?

func loadData(){
    let findDataParse = PFQuery(className: "JobListing")
    findDataParse.findObjectsInBackgroundWithBlock{
        (objects: [PFObject]?, error: NSError?) -> Void in
        if (error == nil) {
            for object in objects! {
               let userImageFile = object["ImageOne"] as! PFFile
                let userImageFile1 = object["ImageTwo"] as! PFFile
                let userImageFile2 = object["ImageThree"] as! PFFile
                userImageFile.getDataInBackgroundWithBlock {
                    (imageData: NSData?, error: NSError?) -> Void in
                    let listingImage1 = UIImage(data:imageData!)
                    userImageFile1.getDataInBackgroundWithBlock {
                        (imageData1: NSData?, error1: NSError?) -> Void in
                        let listingImage2 = UIImage(data:imageData1!)
                        userImageFile2.getDataInBackgroundWithBlock {
                            (imageData2: NSData?, error1: NSError?) -> Void in
                            let listingImage3 = UIImage(data:imageData2!)

    self.flyerImageLarge1.image = listingImage1
    self.flyerImageLarge2.image = listingImage2
    self.flyerImageLarge3.image = listingImage3
}}}}}}}

Image of how I have my PFFile's stored in parse

Upvotes: 0

Views: 53

Answers (1)

Paulw11
Paulw11

Reputation: 114875

It would be better if you used an array column to store your files in Parse because then you could use a loop, but you can retrieve the images in parallel.

The code below shows this, but doesn't actually do anything with the images because it isn't clear what you are trying to do; your code in your question assigns all of the retrieved images to a single UIImageView, so only the last image that is retrieved is actually used.

func loadData(){
    let findDataParse = PFQuery(className: "JobListing")
    findDataParse.findObjectsInBackgroundWithBlock{
        (objects: [PFObject]?, error: NSError?) -> Void in
        if (error == nil) {
            for object in objects! {
               let userImageFile = object["ImageOne"] as! PFFile
               let userImageFile1 = object["ImageTwo"] as! PFFile
               let userImageFile2 = object["ImageThree"] as! PFFile
               userImageFile.getDataInBackgroundWithBlock {
                    (imageData: NSData?, error: NSError?) -> Void in
                    let listingImage1 = UIImage(data:imageData!)
               }

               userImageFile1.getDataInBackgroundWithBlock {
                    (imageData1: NSData?, error1: NSError?) -> Void in
                    let listingImage2 = UIImage(data:imageData1!)
               }

               userImageFile2.getDataInBackgroundWithBlock {
                    (imageData2: NSData?, error1: NSError?) -> Void in
                    let listingImage3 = UIImage(data:imageData2!)
               }
        }
}

Upvotes: 1

Related Questions