2015-11-27 122 views
0

當用戶將文檔上傳到我的分析數據庫時,我需要幫助設置我的if語句。現在,功能起作用。但是我遇到了一個有兩個對象在一定標準下的問題,所以現在當用戶上傳圖片和字符串時,我需要刪除舊對象並保存新對象。這裏是我的代碼:需要幫助設置IF語句iOS Swift

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") 
       })) 
      } 
     } 
    } 
}) 

所以之前我需要刪除舊對象的額外的功能,我postImage.saveInBackgroundWithBlock方法是在我的查詢可以正常使用,只是沒有了。我已經驗證了正確的值被傳入self.userID和self.vaccineDocument,並且在調試後,我可以訪問我需要的對象。我只是覺得我的一些措辭在if object.objectId == nil聲明或其他東西中是錯誤的。任何幫助深表感謝!!

+0

is object.objectId a String?如果是從正確地轉換爲字符串並嘗試string.isEmpty檢查值。 – PK20

+0

感謝您的反饋。所以它object.objectId是一個字符串,所以我開始這樣的發言: let objectString = object.objectId! as String if objectString.isEmpty { 一切編譯正常,但在它爲空的情況下,我的activityIndi​​cator只會旋轉並旋轉,並不會保存解析。如果它不是空的,在我的調試器中會發生同樣的情況我得到這個錯誤: 嘗試在釋放視圖控制器時加載視圖控制器的視圖是不允許的,並且可能導致未定義的行爲() – bme003

+0

我剛纔用一大堆斷點運行我的調試器,基本上我正在檢索我需要的Parse中的位置,只有當objectString不存在時,它纔會跳過所有代碼並保持activityIndi​​cator一直運行。如果它確實存在,它會找到正確的對象,可以訪問它的所有值,但會卡在我的deleteAlert警報控制器中。所以我試了一遍,沒有警報控制器,它的工作原理!它刪除舊對象並保存新對象。但我仍然希望擁有該警報控制器。對我的任何問題有任何新的建議?謝謝! – bme003

回答

0

我發現暫時的工作。而不是使用一個查詢我用2分不同的人,基本上完成了我的IF語句:

let ifActiveQuery = PFQuery(className: "Vaccine") 
ifActiveQuery.whereKey("userID", equalTo: self.userID) 
ifActiveQuery.whereKey("vaccineType", equalTo: self.vaccineDocument) 
ifActiveQuery.getFirstObjectInBackgroundWithBlock({ (objects, error) -> Void in 
    objects?.deleteInBackground() 
    print("deleted object: \(objects)") 

} 

let ifNonExistQuery = PFQuery(className: "Vaccine") 
ifNonExistQuery.whereKey("userID", equalTo: self.userID) 
ifNonExistQuery.whereKey("vaccineType", equalTo: self.vaccineDocument) 
ifNonExistQuery.getFirstObjectInBackgroundWithBlock { (objects, error) -> Void in 

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

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

      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") 

    } 
} 

這確實我希望它只是我不能讓alertController爲刪除工作的情況下工作。