1

Im trying to download some images stored on parse server as PFFile then convert them in UIImage. This is contained within a for loop and the UIImages are appended to an array tempImageArray. This array is populated within the for loop (contains all 9 images on the server) however when i attempt to use this array outside the for loop the array is empty, returns [].

is this something to do with the getdatainbackground and swift is skipping past it before i can use the array tempImagesArray?

code for the whole block is below.

let packQuery = PFQuery(className: "Pack")

packQuery.findObjectsInBackground(block: { (objectsArray, error) in
    if error != nil {
        print(error!)
    } else if let packs = objectsArray {
        var tempImageArray = [UIImage]()
        for object in packs {
            if object.object(forKey: "file") != nil {
                var imageDataDownLoad = object["file"] as! PFFile
                    imageDataDownLoad.getDataInBackground(block: { (imageData, error) in
                        if error == nil {
                            if let image = UIImage(data: imageData!) {
                                tempImageArray.append(image)
                                print(tempImageArray)
                                // this returns a full array
                            }
                        }
                    })
            }
        }
        //self.packImage.append(tempImageArray)
        print(tempImageArray)
        //this returns []
        self.collectionView?.reloadData()
    }
  })

}

----------- EDIT -------------------- used PFImageView from parseUI available on GitHub

packQuery.findObjectsInBackground(block: { (objectsArray, error) in
    if error != nil {
        print(error!)
    } else if let packs = objectsArray {

        for object in packs {

            let arrayName = object.object(forKey: "packName") as! String
            let arrayDescription = object.object(forKey: "packDesctription") as! String
            let arrayTitle = object.object(forKey: "packTitle") as! String
            let arrayImage = object.object(forKey: "file") as! PFFile

            self.packArray.append(packStruct(packName: arrayName, packDescription: arrayDescription, packTitle: arrayTitle, packImage: arrayImage))
        }
        self.collectionView?.reloadData()
    }
})
WanderingScouse
  • 281
  • 1
  • 3
  • 16
  • "is this something to do with the getdatainbackground and swift is skipping past it before i can use the array tempImagesArray?" You are exactly correct. See suggestions on http://stackoverflow.com/questions/28488867/read-multiple-data-in-block-method-ios – Palpatim Dec 22 '16 at 22:58
  • thanks, i ended up working around this and importing the PFImageView classes from the parseUI project on GitHub and using that. though even this was a work around as the cocoapod version didn't work and just had to import the individual .h and .m files to make it work. – WanderingScouse Dec 23 '16 at 00:42

1 Answers1

0
    packQuery.findObjectsInBackground(block: { (objectsArray, error) in
        if error != nil {
            print(error!)
        } else if let packs = objectsArray {

            for object in packs {

                let arrayName = object.object(forKey: "packName") as! String
                let arrayDescription = object.object(forKey: "packDesctription") as! String
                let arrayTitle = object.object(forKey: "packTitle") as! String
                let arrayImage = object.object(forKey: "file") as! PFFile

                self.packArray.append(packStruct(packName: arrayName, packDescription: arrayDescription, packTitle: arrayTitle, packImage: arrayImage))
            }
            self.collectionView?.reloadData()
        }
    })
WanderingScouse
  • 281
  • 1
  • 3
  • 16