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

标签 ios swift parse-platform

如果产品(代码中的标题)已经存在于“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/

相关文章:

iphone - iOS6 上的 IOHIDEventSystemCreate 失败

swift - iOS 12/Swift 强密码自动填充无法按预期工作

android - 使用 android 和 parse.com 进行缓慢的数据检索

ios - 解析 (iOS) : User cannot be deleted unless they have been authenticated via logIn or signUp

ios - NotSupportedException get_HasShutdownStarted - “il2cpp 不支持此 icall

ios - 当我切换到 TabBarController 内的另一个 ViewController 时,后退按钮不起作用

ios - 如何在 Swift 3.0 中将 JSON 输出设置为 UILabel ?

ios - 将 C 转换为 Swift : Add magnifying glass icon to UITextField

ios - 解析单个字符串字段的查询,其中值与数组中提供的任何字符串匹配

ios - 从另外两个核心数据对象创建复合对象