so I am having trouble understanding how a PFRelation saves, I have this code:
PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:#"likes"];
[relation addObject:post];
[user saveInBackground];
Why is it that updating the user i.e. [user saveInBackground] updates the PFRelation field "likes" for that user? Does this use a single API call or does [relation addObject: post]; also require an API call?
Any help would be greatly appreciated.
Thanks
In your case 1 API request is used under circumstances.
Take a look at PFObject [straight from Parse.com]:
// Create the post
PFObject *myPost = [PFObject objectWithClassName:#"Post"];
myPost[#"title"] = #"I'm Hungry";
myPost[#"content"] = #"Where should we go for lunch?";
// Create the comment
PFObject *myComment = [PFObject objectWithClassName:#"Comment"];
myComment[#"content"] = #"Let's do Sushirrito.";
// Add a relation between the Post and Comment
myComment[#"parent"] = myPost;
Here you are setting attributes or properties to the PFObject but nothing ever happens until you save it, you can do anything to the object like change it, update it, doesn't matter, but backend wise, it won't update unless you tell it to which is where save comes in play :
[myComment saveInBackground];
In short, you can add relations, pointers and numerous parameters all day long, but nothing happens until you tell it to happen : [saveInBackground];
Because you made it a direct correlation to user it saves it to that user because you told it to. Because you specified a relation to the user, once you save the user properties the relation will also be saved. However, this doesn't create more API requests.
Related
in my project I have a user relation for the key userPosts. I want to add to this relation different objects with different class names, something like this:
PFObject *post = [PFObject objectWithClassName:selectedCategory];
// doing stuff with the object
[object save];
PFRelation *relation = [PFUser currentUser] relationForKey:#"userPosts"];
[relation addObject:post];
[PFUser currentUser] saveInBackground];
The problem is that when I'm trying to execute this code it throws me a runtime error saying basically that I can only add objects to a relation with one class name.
How do I override/fix it? Thanks
in my project I have a user relation for the key userPosts. I want to add to this relation different objects with different class names, in this example a category from a UIPicker, something like this:
PFObject *post = [PFObject objectWithClassName:selectedCategory];
//do stuff with the object...
[object save];
PFRelation *relation = [PFUser currentUser] relationForKey:#"userPosts"];
[relation addObject:post];
[PFUser currentUser] saveInBackground];
The problem is that when I'm trying to execute this code it throws me a runtime error saying basically that I can only add objects to a relation with one class name. How do I override/fix it? Thanks :)
I want to retrieve an array from Parse.com. The array is saved in the user class in Parse, and I would like to retrieve only the array linked to the current user.
I know this code is not valid, but I think it illustrates exactly what I want:
NSArray *array = [[PFUser currentUser] arrayForKey:#"arrayKey"];
I know this code snippet might seem stupid for some of you, but I hope you get what I'm trying to do.
PFUser* currentUser = [PFUser currentUser];
NSArray* myArray = currentUser[#"arrayKey"];
I'm using Parse for the back end in my project.
As you would imagine there are quite a few relations in the data model. A lot of the time I create a "parent" object and all of its "children" at the same moment and save them all to Parse.
Now, when doing this is it necessary to save the children individually? The same for files etc...
First example - Adding an avatar to a user object
UIImage *image = // image from camera
NSData *pngData = UIImagePNGRepresentation(image);
PFFile *imageFile = [PFFile fileWithData:pngData];
[[PFUser currentUser] setObject:imageFile forKey:"avatar"];
OK, so on the device I can reference the #"avatar" key on the user and get the avatar file. But how should this be saved to Parse?
If I do...
[[PFUser currentUser] saveInBackground];
Will this save the new file that was added? Or do I need to save the file first and wait for this to succeed before then adding it into the user object and then saving the user object?
Second example
Creating a tree of objects...
PFObject *family = [PFObject objectWithClassName:#"Family"];
[family setObject:#"Smith" forKey:#"familyName"];
PFObject *person1 = [PFObject objectWithClassName:#"Person"];
[person1 setObject:#"Bob" forKey:#"name"];
PFObject *person2 = [PFObject objectWithClassName:#"Person"];
[person2 setObject:#"Alice" forKey:#"name"];
PFObject *person3 = [PFObject objectWithClassName:#"Person"];
[person3 setObject:#"Chris" forKey:#"name"];
[family setObject:#[person1, person2, person3] forKey:#"members"];
How would I save this collection of objects?
Can I just do [family saveInBackground];?
Or do I have to go through a process of saving every Person object first and checking that it worked before saving the family object?
As long as the relationship between parent and child is Pointer, you don't have to save the child first. PFRelation works differently, but a save on the parent object will also save children related as pointers. This is true for Cloud Code, and I am pretty sure it holds true for the device as well.
Some details in this answer: https://www.parse.com/questions/cloud-code-efficient-hierarchy-saving
Yes that will do...in fact there is a wide range of methods for saving the contents
Saving an Object to Parse
– save
– save:
– saveInBackground
– saveInBackgroundWithBlock:
– saveInBackgroundWithTarget:selector:
– saveEventually
– saveEventually:
Saving Many Objects to Parse
+ saveAll:
+ saveAll:error:
+ saveAllInBackground:
+ saveAllInBackground:block:
+ saveAllInBackground:target:selector:
Refer Here for more
i am newbiew in Parse. I'm trying to learn the following this link. https://www.parse.com/docs/ios_guide#objects-updating/iOS
Here is the updated code does not work and creating a new one. This is my code;
PFObject *testObject = [PFObject objectWithClassName:#"TestObject"];
[testObject setObject:#"oldtestobject" forKey:#"testkey"];
[testObject save];
I just want to update the oldtestobject string. Get the new value of newtest.
I'm sorry for my english is bad. I hope what I'm saying.
If you want to update a object with parse, you will first need to create a PFQuery to obtain the object you wish to update, then modify the data, then save the new version. Here is what the sample code may look like:
// Create the PFQuery
PFQuery *query = [PFQuery queryWithClassName:#"TestObject"];
// Retrieve the object by id
[query getObjectInBackgroundWithId:#"oldTestObjectID" block:^(PFObject *pfObject, NSError *error) {
// Now let's update it with some new data. In this case, only cheatMode and score
// will get sent to the cloud. playerName hasn't changed.
[pfObject setObject:#"rowData" forKey:#"columnName"];
[pfObject saveInBackground];
}];