Array of PFFile cast to UIImage is empty outside of for loop

45 views Asked by At

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()
    }
})
1

There are 1 answers

0
WanderingScouse On
    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()
        }
    })