gpt4 book ai didi

ios - 如何用Parse解决Swift中 "no results matched the query"的错误

转载 作者:行者123 更新时间:2023-11-28 08:49:04 24 4
gpt4 key购买 nike

如果产品(代码中的标题)已经存在于“grouped_products”类中,我会尝试将 (detailsObj) 添加到 username_list 中;否则,我会在我的类(class)和 (detailsObj) 中添加一个产品。一切正常,但显示“没有匹配查询的结果”的错误。这对我来说很有意义,因为如果没有匹配查询的结果,我会在我的类(class)中添加一个产品。但是,我不知道有什么办法可以解决这个错误?

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

最佳答案

PFQuery documentation对于 getFirstObjectInBackgroundWithBlock: 状态

The block to execute. It should have the following argument signature: ^(PFObject *object, NSError *error). result will be nil if error is set OR no object was found matching the query. error will be nil if result is set OR if the query succeeded, but found no results.

显然该文档至少是 partly wrong关于没有找到结果的错误 nil

因此,您可能应该使用 findObjectsInBackgroundWithBlock: 代替,因为如果没有找到匹配项,数组将只是空的,但不会出现 error(它将是 ).

如果您仍想将结果限制为只有一个,请将 limit 指定为 1

关于ios - 如何用Parse解决Swift中 "no results matched the query"的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34565786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com