Reputation: 621
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.
Upvotes: 0
Views: 114