lundi 23 novembre 2015

My IF statement will only run one condition - iOS Swift

I'm trying to save a new object to my parse database and basically if a user is uploading a document and a document already exists for the specific criteria, I want to delete the old and update with the new. Here is my code:

if object.objectId == nil {

     postImage.saveInBackgroundWithBlock {
         (succeeded: Bool, error: NSError?) -> Void in

          self.activityIndicator.stopAnimating()
                         UIApplication.sharedApplication().endIgnoringInteractionEvents()

          if let error = error {

             if let errorString = error.userInfo["error"] as? String {
                                    self.displayAlert("Please choose an image and expiration date", message: errorString)
          }

          } else {

             print("Saved new object \(postImage)")
             print("Saved new object:\(self.expirationField.text)")

             self.dismissViewControllerAnimated(true, completion: nil)

             self.expirationField.text = ""
             self.imageToPost.image = UIImage(named: "addDocument.jpg")
         }
     }

 } else {

        object.deleteInBackground()

        postImage.saveInBackgroundWithBlock {
              (succeeded: Bool, error: NSError?) -> Void in

             self.activityIndicator.stopAnimating()
                            UIApplication.sharedApplication().endIgnoringInteractionEvents()

             if let error = error {

                if let errorString = error.userInfo["error"] as? String {
                                    self.displayAlert("Please choose an image and expiration date", message: errorString)
             }

             } else {

                print("Saved new object \(postImage)")
                print("Deleted old object and added:\(self.expirationField.text)")

                self.dismissViewControllerAnimated(true, completion: nil)

                self.expirationField.text = ""
                self.imageToPost.image = UIImage(named: "addDocument.jpg")
             }
        }
 }

So what ends up happening is that it never runs the first condition, but the app compiles fine. If in the simulator it matches the first condition it just has my activity indicator that runs forever and doesn't save to parse. Any help is much appreciated!!

Aucun commentaire:

Enregistrer un commentaire