2016-03-02 124 views
-1

在我的應用程序中,用戶可以添加嬰兒名稱。我想確保這個寶貝之前沒有添加過,所以我可以添加他。如果他在那裏,會出現一條錯誤消息,告訴他如此。 這裏的問題,我不能檢查這個嬰兒是否被添加。用於檢查的代碼從不執行。我不知道爲什麼。 我試圖在很多方面找出問題所在。但是,當檢查代碼被忽略時,警報消息會以黑屏顯示,並以任何方式添加寶寶!檢查此對象是否存在

這是我的代碼,這段代碼是否保存寶貝是否加入。

let addBaby = PFObject(className: "Baby") 

    let CheckBaby = PFQuery(className: "Baby") 
    CheckBaby.whereKey("user1", equalTo: ParentID!) 
    CheckBaby.whereKey("user2", equalTo: PFUser.currentUser()!) 
    CheckBaby.findObjectsInBackgroundWithBlock({ (object:[PFObject]?, 
     error:NSError?) -> Void in 

     for object in object! { 
      if object["baby_name"] as! String == self.babyName { 
       print("This baby has been added before.") 
       let myAlert = UIAlertController(title:"Error", message:"This baby has been added before.", preferredStyle:UIAlertControllerStyle.Alert) 
       let okAction = UIAlertAction (title: "OK", style: UIAlertActionStyle.Default, handler: nil) 
       myAlert.addAction(okAction) 
       self.presentViewController(myAlert, animated: true, completion: nil) 
    return} 
      else{ 
      } 
     }// end for 
    }) 

    addBaby.setObject(self.ParentID!, forKey: "user1") 
    addBaby.setObject(self.babyName.text!, forKey: "baby_name") 
    addBaby.setObject(self.parentName.text!, forKey: "parent_fname") 
    addBaby.setObject(self.babyClass.text!, forKey: "class_name") 
    addBaby.setObject(PFUser.currentUser()!, forKey: "user2") 


    addBaby.saveInBackgroundWithBlock { (saved:Bool, error:NSError?) -> Void in 

     if saved { 
      self.navigationController?.popViewControllerAnimated(true) 
     } else { 
      error!.localizedDescription 
     } 
    } 

謝謝你的幫助!

回答

0

無論查找結果如何,您的代碼都在添加寶貝。您的代碼基本上是這樣說:

Create add baby 
Find a baby like this 
    If there is one like this, display alert 
Setup baby to be added 
Add the baby 
    If it works, popview 

你想要的是:

Find a baby like this 
    If there is one, display alert 
    If there isn't one 
     Create baby to add 
     Setup baby to be added 
     Add the baby 
      If it works, pop view 

喜歡的東西:

let CheckBaby = PFQuery(className: "Baby") 
CheckBaby.whereKey("user1", equalTo: ParentID!) 
CheckBaby.whereKey("user2", equalTo: PFUser.currentUser()!) 
CheckBaby.findObjectsInBackgroundWithBlock({ (object:[PFObject]?, 
    error:NSError?) -> Void in 

    var babyNotFound = true 

    for object in object! { 
     if object["baby_name"] as! String == self.babyName { 

      babyNotFound = false 

      print("This baby has been added before.") 
      let myAlert = UIAlertController(title:"Error", message:"This baby has been added before.", preferredStyle:UIAlertControllerStyle.Alert) 
      let okAction = UIAlertAction (title: "OK", style: UIAlertActionStyle.Default, handler: nil) 
      myAlert.addAction(okAction) 
      self.presentViewController(myAlert, animated: true, completion: nil) 
      return 
     } 
    } 

    if babyNotFound { 
     let addBaby = PFObject(className: "Baby") 

     addBaby.setObject(self.ParentID!, forKey: "user1") 
     addBaby.setObject(self.babyName.text!, forKey: "baby_name") 
     addBaby.setObject(self.parentName.text!, forKey: "parent_fname") 
     addBaby.setObject(self.babyClass.text!, forKey: "class_name") 
     addBaby.setObject(PFUser.currentUser()!, forKey: "user2") 


     addBaby.saveInBackgroundWithBlock { (saved:Bool, error:NSError?) -> Void in 

      if saved { 
       self.navigationController?.popViewControllerAnimated(true) 
      } else { 
       error!.localizedDescription 
      } 
     } 
    } 
}) 

注:我剛纔重組你的代碼,它仍然是需要的可讀性的變化使其在生產應用中可用。

還有你描述的黑點的問題,這可能是因爲你在後臺線程上做UI代碼而發生的。 UI更新應該在主線程上。

dispatch_async(dispatch_get_main_queue()) { 
    () -> Void in 
    self.presentViewController(myAlert, animated: true, completion: nil) 
} 
+0

同樣的問題!此代碼不工作,並以任何方式添加寶寶!如果object [「baby_name」]爲! String == self.babyName { babyNotFound = false print(「此嬰兒已被添加過。」) let myAlert = UIAlertController(標題:「錯誤」,消息:「此嬰兒已添加過。」,preferredStyle: UIAlertControllerStyle.Alert) let okAction = UIAlertAction(title:「OK」,style:UIAlertActionStyle.Default,handler:nil) myAlert.addAction(okAction) self.presentViewController(myAlert,animated:true,completion:nil)return} –

+0

Yaay,它的工作!我嘗試了你的建議解決方案和dispatch_async函數。另外,我意識到我忘了在比較中放置self.babyName.text。 –

+0

非常感謝你! –