0

I have a large class called "Goal" in parse. This class has multiple elements, one of which is a PFFile, that is always a UIImage.

When I perform my query for the "Goal" class, I cannot figure out how to take the PFFile, and change it to a UIImage for use.

         var query = PFQuery(className:"Goal")

    let currentUser = PFUser.currentUser()!.username
    query.whereKey("creator", equalTo: currentUser!)
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            // The find succeeded.
            println("Successfully retrieved \(objects?.count) goals for the TableView.")
            // Do something with the found objects
            if let objects = objects as? [PFObject] {
                for object in objects {

                    let goalType = object["type"] as! String
                    let goalPeriod = object["period"] as! String
                    let goalCategory = object["category"] as! String
                    let goalShortDescription = object["shortDescription"] as! String
                    let goalLongDescription = object["longDescription"] as! String
                    let goalPointvalue = object["pointValue"] as! Int
                    let goalSharedSetting = object["shared"] as! Bool
                    let goalAdoptionCount = object["adoptionCount"] as! Int
                    let goalIsComplete = object["isComplete"] as! Bool

                    let goalSuccessImageData = object["image"] as! PFFile

                    goalSuccessImageData.getDataInBackgroundWithBlock {
                        (imageData: NSData?, error: NSError?) -> Void in
                        if error == nil {
                            if let imageData = imageData {
                                let image = UIImage(data:imageData)
                                self.imageQuery = image
                            }
                        }
                    }
                    let goalSuccessImage : UIImage = self.imageQuery

                    let goalObjectID = object.objectId
                    let goalSpreadCount = object["spreadCount"] as! Int
                    let goalSpreadTotal = object["spreadTotal"] as! Int
                    let goalTotalCompletions = object["totalCompletions"] as! Int


                    let thisGoal = GoalModel(period: goalPeriod, type: goalType, category: goalCategory, shortDescription: goalShortDescription, longDescription: goalLongDescription, pointValue: goalPointvalue, shared: goalSharedSetting, adoptionCount: goalAdoptionCount, isComplete: goalIsComplete, successImage: goalSuccessImage, goalID: goalObjectID!, spreadCount: goalSpreadCount, spreadTotal: goalSpreadTotal, totalCompletions: goalTotalCompletions ) as GoalModel

any tips on how to modify the "success image" part? I added a space before and after to make it easier to find.

Thank you in advance!

Mason Ballowe
  • 1,847
  • 2
  • 12
  • 23
  • You need to download the data of the PFFile itself, see http://stackoverflow.com/a/27596932/2227743 – Eric Aya May 04 '15 at 10:40
  • so what is the object["image"] returning if not the data? – Mason Ballowe May 04 '15 at 13:09
  • It returns the instance of the PFFile object, which contains methods to get the data. A PFFile *represents* an object with data, it's not the data itself. See https://parse.com/docs/osx/api/Classes/PFFile.html – Eric Aya May 04 '15 at 13:13
  • i tried the new code above and still got an error. I don't know how to pass the image created by the query out of the query and into the thisGoal : GoalModel – Mason Ballowe May 04 '15 at 13:46
  • 1
    Because you've added a *block* for downloading the image, and this block is asynchronous, so everything after it is executed *before* the block succeeds: when you create `goalSuccessImage`, the block `goalSuccessImageData` hasn't downloaded the data yet. – Eric Aya May 04 '15 at 13:50
  • so how should i modify it? – Mason Ballowe May 04 '15 at 13:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76896/discussion-between-mason-ballowe-and-ericd). – Mason Ballowe May 04 '15 at 13:54
  • 1
    thank you EricD. I moved the code to another spot and it worked perfectly! (more testing to come, haha) – Mason Ballowe May 04 '15 at 14:36

1 Answers1

1

I'm using this way in my projects, if it help's you :

func performSave(sender: UIBarButtonItem){
    affichageActivityIndicator()
    let qos = Int(QOS_CLASS_USER_INITIATED.value)
    dispatch_async(dispatch_get_global_queue(qos,0)) { () -> Void in
        dispatch_async(dispatch_get_main_queue()){
            if let updateObject = self.currentObject as PFObject? {
                let imageData = UIImageJPEGRepresentation(imageToSave, 0.1)
                let imageFile = PFFile(name:"image.png", data:imageData)

                updateObject["imageFile"] = imageFile

                // Save the data back to the server in a background task
                updateObject.saveInBackgroundWithBlock{(success: Bool, error: NSError!) -> Void in
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                    if success == false {
                        println("Error")
                    }
                }
            }
        }
    }
}