jeudi 26 novembre 2015

Need help setting up IF statement iOS Swift

I need help setting up my if statement while users upload a document to my parse database. Right now, the functionality works. But I came across a problem with having 2 objects under a certain criteria, so now when a user goes to upload an image and string, I need to delete the old one and save the new one. Here is my code:

let postImage = PFObject(className: "Vaccine")
postImage["expiration"] = expiration.text

let imageData = UIImageJPEGRepresentation(imageToPost.image!, 0.2)
let imageFile = PFFile(name: "dogumentImage.jpg", data: imageData!)
postImage["dogumentImage"] = imageFile

let ifActiveQuery = PFQuery(className: "Vaccine")
ifActiveQuery.whereKey("userID", equalTo: self.userID)
ifActiveQuery.whereKey("vaccineType", equalTo: self.vaccineDocument)
ifActiveQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
    if let vaccine = objects {
        for object in vaccine {
            print(object.objectId)
            if object.objectId == nil {
                postImage.saveInBackgroundWithBlock {
                    (succeeded: Bool, error: NSError?) -> Void in
                    self.activityIndicator.stopAnimating()
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                    self.dismissViewControllerAnimated(true, completion:nil)
                    self.expirationField.text = ""
                    self.imageToPost.image = UIImage(named: "addDocument.jpg")
                }
            } else {
                let deleteAlert = UIAlertController(title: "Update Dogument Information?", message: "All old data will be lost.", preferredStyle: UIAlertControllerStyle.Alert)
                deleteAlert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction!) in
                    object.deleteInBackground()
                    postImage.saveInBackgroundWithBlock {
                        (succeeded: Bool, error: NSError?) -> Void in
                        self.activityIndicator.stopAnimating()
                        UIApplication.sharedApplication().endIgnoringInteractionEvents()
                        self.dismissViewControllerAnimated(true, completion: nil)
                        self.expirationField.text = ""
                        self.imageToPost.image = UIImage(named: "addDocument.jpg")
                    }
                }))
                deleteAlert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in
                        print("Canceled Delete")
                }))
            }
        }
    }
})

So before I needed the extra functionality of deleting the old object, my postImage.saveInBackgroundWithBlock method was working perfectly, just not anymore in my query. I've verified that the right values are being passed in for the self.userID and self.vaccineDocument and after debugging, I do have access to the objects I need. I just think some of my wording is wrong in the if object.objectId == nil declaration or something. Any help is much appreciated!!

Aucun commentaire:

Enregistrer un commentaire