0

I know this question has been asked around quite a bit, but none of the solutions are fitting my scenario. I am trying to add an object to a relation for the current user. Note that I have no issues adding object to relation unless the object that has a PFRelation is a PFUser.

The following works fine:

let testChild: PFObject = PFObject(className: "TestChild")
testChild["name"] = "test"
testChild.saveInBackgroundWithBlock { (success, error) -> Void in
  let testParentQuery: PFQuery = PFQuery(className: "TestParent")
  testParentQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
    let testParent: PFObject = (objects?.first)!
    let testRelation: PFRelation = testParent.relationForKey("children")
    testRelation.addObject(testChild);
    testParent.saveInBackground()
   })
}

But this doesn't work:

let testChild2: PFObject = PFObject(className: "TestChild")
testChild2["name"] = "test"
testChild2.saveInBackgroundWithBlock { (success, error) -> Void in
  let testRelation: PFRelation = (PFUser.currentUser()?.relationForKey("testRelation"))!
  testRelation.addObject(testChild2)
  PFUser.currentUser()?.saveInBackground()
}

I checked/double-checked PFUser.currentUser() returns a valid value, testRelation exists with a proper name, type etc. Also TestChild object saves just fine, it's PFUser.currentUser()?.saveInBackground() that logs errors

"can't add a non-pointer to a relation (Code: 111, Version: 1.10.0)" as if I was trying to set a string or something for the relation, although I clearly am not. Also, I have no issues setting/saving non-relation fields on a user (like strings). I am subclassing both PFUser and the object I am trying to add to a relationship, but trying to narrow the issue down, I tried exactly the code above and using just PFUser and PFObject in code.

What am I doing wrong? Please help, I've been stuck on it for a couple of hours already, I just can't make any sense of what's going on.

Juri Noga
  • 4,363
  • 7
  • 38
  • 51
ITGronk
  • 621
  • 1
  • 6
  • 13
  • Does `testChild2` have an object id when you try to add it to the relation? – Wain Dec 10 '15 at 12:06
  • yes, it is saved in DB and has an id, I logged it right before testRelation.addObject(testChild2)PFUser.currentUser()?.saveInBackground() – ITGronk Dec 10 '15 at 12:09
  • @Wain are you able to add to a relation using similar code? Which version of parse/facebook SDK are you using? I am pulling the latest with cocoa pods – ITGronk Dec 10 '15 at 12:21
  • I have done this in swift recently, though I haven't actually run the code in a couple of months as I'm working on another project. Parse error codes aren't particularly revealing about the real problem... What are the ACLs on the user and child classes and instances? – Wain Dec 10 '15 at 13:48
  • Ok, i think we are somewhat on the right track here child classes are public readwrite, user is public read. I have two questions now is how am I able to set/save other fields on user object/table. Also how can I modify ACL on user? I am using Facebook to log in. – ITGronk Dec 10 '15 at 14:02
  • as it's the current user you should have permission based on what you say – Wain Dec 10 '15 at 14:07

0 Answers0