Object not found for update error Parse Swift

妖精的绣舞 提交于 2019-12-12 04:18:01

问题


I'm using parse on Xcode 7.2 and I'm having trouble updating the object with swift. I want to update my boolean value in parse class column "Approve" and my class name is "Request" and I have "FromUserName" column which is type String and "ToUsername" column which is String as well

This is my code in swift

 func block(){
    let name = self.userDelName
    let query = PFQuery(className: "Request")
    //let username = PFUser.currentUser()?.username!
    if let username = PFUser.currentUser()?.username! {
         query.whereKey("ToUsername", equalTo: username)
    }
    query.whereKey("FromUsername", equalTo: name)
    print(name+" IMHEHEHEHEHERRRR")
    query.getFirstObjectInBackgroundWithBlock{(object,error) -> Void in
        if error == nil {
                print(object)
                object!["Approve"] = false
                object!.saveInBackgroundWithBlock {(success: Bool, error: NSError?) -> Void in
                    if (success) {
                       print("success")
                    } else {
                        print("down")
                       print(error)
                    }
                }

               }
        }

    }

and the error is

[Error]: object not found for update (Code: 101, Version: 1.11.0)

I don't know what to do now.

Any help is appreciated. Thank you

EDIT: this is my saved method

requested = PFObject(className: "Request")
        requested!["From"] = PFUser.currentUser()
        requested!["FromUsername"] = PFUser.currentUser()?.username
        requested!["ToUsername"] = user
        requested!["To"] = newFriend
        requested!["Approve"] = true
         requested!.saveInBackgroundWithBlock {(success: Bool, error: NSError?) -> Void in
            if error == nil {
               print("success")
            }
            else{
                print(error)
            }
        }

回答1:


Try changing your save code to this:

    requested = PFObject(className: "Request")
    requested!["From"] = PFUser.currentUser()
    requested!["FromUsername"] = PFUser.currentUser()?.username
    requested!["ToUsername"] = user
    requested!["To"] = newFriend
    requested!["Approve"] = true
    requested.ACL?.setPublicWriteAccess(true)
     requested!.saveInBackgroundWithBlock {(success: Bool, error: NSError?) -> Void in
        if error == nil {
           print("success")
        }
        else{
            print(error)
        }
    }

The line:

 ACL?.setPublicWriteAccess(true)

will set it so anyone can edit to the object though. That may not be exactly what you want. There are other methods too that can allow someone to write over an object. You may be able to use them depending on your needs and how your Parse backend is configured:

    object.ACL?.setWriteAccess(allowed: Bool, forRole: PFRole)
    object.ACL?.setWriteAccess(allowed: Bool, forRoleWithName: String)
    object.ACL?.setWriteAccess(allowed: Bool, forUser: PFUser)
    object.ACL?.setWriteAccess(allowed: Bool, forUserId: String)

Also note that this will not work for current objects in parse. Only for objects saved after adding the setWriteAccess method. If you want to edit current objects in parse, you have to change the read and write permissions form the database itself, manually.



来源:https://stackoverflow.com/questions/34671553/object-not-found-for-update-error-parse-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!