简体   繁体   English

如何使用Parse解决Swift中“没有结果与查询匹配”的错误

[英]how to solve the error of “no results matched the query” in Swift with Parse

I try to add (detailsObj) into username_list if a product (title in the code) already exist in the class of "grouped_products"; 我尝试将(detailsObj)添加到username_list中,如果“ grouped_products”类中已经存在某种产品(代码中的标题); otherwise, I would add a product into my class and (detailsObj) as well. 否则,我会将产品添加到我的课程中,以及(detailsObj)。 Everything works well, but the error of "no results matched the query" showed. 一切正常,但是显示“没有结果与查询匹配”的错误。 It makes sense to me because if no results matched the query, I will add a product into my class. 这对我来说很有意义,因为如果没有结果与查询匹配,则将产品添加到类中。 But, I don't know is there any way I can solve this error? 但是,我不知道有什么方法可以解决此错误?

let detailsObj = PFObject(className: "products")
let title = 1 <<==(example)

 ........
let query = PFQuery(className: "grouped_products")
query.whereKey("title", equalTo: title)
query.getFirstObjectInBackgroundWithBlock{ (titleInParse: PFObject?, error:NSError?) -> Void in
    if titleInParse != nil{
        titleInParse!.addObjectsFromArray([detailsObj], forKey: "username_list")
        titleInParse?.saveInBackgroundWithBlock({ (success: Bool?, error:NSError?) -> Void in
            loadingNotification.hide(true)
            if (error != nil){
                self.appDelegate.displayMyAlertMessage(error!.localizedDescription, userVI: self)
                return
            } else {
                self.appDelegate.displayMyAlertMessage("Sent", userVI: self)
            }
        })
    } else if titleInParse == nil {
        let grouped_product = PFObject(className: "grouped_products")
        grouped_product["title"] = title
        grouped_product.addUniqueObject(detailsObj, forKey: "username_list")
        grouped_product.saveInBackgroundWithBlock({ (success: Bool?, error:NSError?) -> Void in
            if (error != nil){
                self.appDelegate.displayMyAlertMessage(error!.localizedDescription, userVI: self)
                return
            } else {
                self.appDelegate.displayMyAlertMessage("Sent", userVI: self)
            }
        })
    } else {
    }
}

The PFQuery documentation for getFirstObjectInBackgroundWithBlock: states getFirstObjectInBackgroundWithBlock:PFQuery文档 getFirstObjectInBackgroundWithBlock:状态

The block to execute. 要执行的块。 It should have the following argument signature: ^(PFObject *object, NSError *error). 它应具有以下参数签名:^(PFObject * object,NSError * error)。 result will be nil if error is set OR no object was found matching the query. 如果设置了错误,或者找不到匹配查询的对象,则结果将为nil。 error will be nil if result is set OR if the query succeeded, but found no results. 如果设置了result或如果查询成功但未找到结果,则错误将为nil。

Apparently that documentation is at least partly wrong in regards to the error being nil for no found results. 显然,该文档是至少部分错误的问候错误是nil对未发现的结果。

Therefore you should probably use findObjectsInBackgroundWithBlock: instead because if no match is found, the array will simply be empty but no error will be present (it will be nil ). 因此,您可能应该使用findObjectsInBackgroundWithBlock:因为如果找不到匹配项,则该数组将只是空的,但不会error (它将为nil )。

If you still want to limit the results to just one you specify a limit of 1 . 如果仍然要将结果限制为一个,则将limit指定为1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM