检查此对象之前是否存在

时间:2016-03-02 22:06:24

标签: ios swift parse-platform uialertcontroller

在我的应用中,用户可以添加婴儿名称。我想确保之前没有添加这个宝贝,所以我可以加他。如果他在那里,会出现一条错误消息。 这里的问题,我无法检查这个宝宝是否被添加。从不执行检查代码。我不知道为什么。 我试图以多种方式解决问题。但是当检查代码生效时,警报信息会出现黑屏,并以任何方式添加婴儿!

这是我的代码,此代码以任何方式保存宝宝是否添加。

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

感谢您的帮助!!

1 个答案:

答案 0 :(得分: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)
}