Alan Lee Alan Lee - 3 months ago 45
iOS Question

Updating object in Parse iOS, [Error]: Object not found

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

// Set up the Parse SDK
let configuration = ParseClientConfiguration {
$0.applicationId = "WhatsTheHW"
$0.server = "https://whatsthehw-parse-alan.herokuapp.com/parse"
}
Parse.initializeWithConfiguration(configuration)

let query = PFQuery(className: "Course")

query.findObjectsInBackgroundWithBlock {(result: [PFObject]?, error: NSError?) -> Void in
for object in result! {
// existing objectIds: 1Ja2Hx77zA, 34AF1vKO6f, 5FWlsswxw0
if object.objectId == "34AF1vKO6f" {
object["studentRelation"] = ["hi", "ih"]
object.saveInBackgroundWithBlock{(success, error) in
if success == true {
print("\(object) saved to parse")
} else {
print("save failed: \(error)")
}
}
}
}
}

return true
}


This is the minimum I can reduce this task to (this code is at AppDelegate).

It all worked fine when I tried using REST api and api console in parse dashboard but it doesn't work with iOS sdk.

The error I'm getting from the print statement is

Error Domain=Parse Code=101 "Object not found." UserInfo={code=101, temporary=0, error=Object not found., NSLocalizedDescription=Object not found.}


It works if I'm simply adding a new object like this :

let object = PFObject(className: "Course")
object["name"] = "German"
object["studentRelation"] = ["a", "b"]

object.saveInBackgroundWithBlock{(success, error) in
if success == true {
print("save completed")
print("\(object) saved to parse")
} else {
print("save failed: \(error)")
}
}


I'm really lost and I don't know why this is happening.

Thanks in advance.

Answer

This issue might relate to the access rights (ACL) of the object that you're trying to save. [Error]: Object not found is printed when a user that doesn't have right access to an object tries to save it, the error message of the Parse SDK is really misleading here!

Make sure the parse user who is trying to save the object has the proper writes to actually write on that object in your parse DB.

An easy fix will be to set the default ACL inside your app to public read + write:

let acl = PFACL()
acl.publicReadAccess = true
acl.publicWriteAccess = true
PFACL.setDefaultACL(acl, withAccessForCurrentUser: true)

Be careful with this approach though, usually you want to set access rights according to the actual role of the user. So a better alternative would be to only set the ACL on the PFObject when you're creating it and only give write access to the users you know should be able to alter the object.

Comments