Object not found for update error Parse Swift

2019-09-01 15:47发布

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条回答
看我几分像从前
2楼-- · 2019-09-01 16:23

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.

查看更多
登录 后发表回答