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];
}];
Related
I'm trying to make a follow action with parse on my ios9 project but i have a little problem when retreiving the PFUser from parse using objectId can anyone help me please (the problem is in adding the user ID of current user to the user followed )
- (IBAction)followButtonAction:(id)sender {
PFQuery * query = [PFUser query];
[query whereKey:#"objectId" equalTo:_a.userId];
NSArray *objects = [query findObjects];
PFUser *user= objects.firstObject;
//add the user ID for the cell we are following to the array of followed items in the user class in parse
[[PFUser currentUser] addUniqueObject:_a.userId forKey:#"followed"];
[[PFUser currentUser] saveInBackground];
//add the user ID to the user that the user followed
[user addUniqueObject:[PFUser currentUser].objectId forKey:#"followers"];
[user saveInBackground];
}
Assuming your question is about the long-running operation (as per your comment):
Your query is being executed on the main thread, because you called [query findObjects]. Use findObjectsInBackgroundWithBlock instead, and place all the code after that inside the callback.
I have a table called "UserSnapshot" on Parse and of course you get the objectID's as you populate the table.
However, when I query the table for an object from my app I wont have the object ID's but I will have their "UserCode". I have been playing with something like this.
PFQuery *userProfile = [PFQuery queryWithClassName:#"UserSnapshot"];
[userProfile whereKey:#"Code" equalTo:_Code];
[userProfile getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!object) {
// Did not find PFObject
// not executed
} else {
// Found PFObject
// also not executed....huh?
}
}];
But nothing happens. Neither the if or the else is entered. Am I missing something?
Thanks
Does anything print out in the log/console? It's possible that you didn't set your keys properly when initializing Parse in your App Delegate.
PFQuery *userProfile = [PFQuery queryWithClassName:#"UserSnapshot"];
[userProfile whereKey:#"Code" equalTo:_Code];
PFObject *object = [userProfile getFirstObject];
Works!
I'm trying to add an object to a relation in Parse, although the code gets exectued without any errors the relation does not appear in the backend, therefore the object was not saved.
PFObject *newContact = [PFObject objectWithClassName:#"Contact"];
[newContact saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
PFQuery *query = [PFQuery queryWithClassName:#"Trip"];
PFObject *trip = [query getObjectWithId:self.parseID];
PFRelation *rel = [trip relationForKey:#"contacts"];
[rel addObject:newContact];
contact.parseID = newContact.objectId;
}];
I've also checked if the PFObject trip is correct and I get back the desired object with the corresponding id. Also the key contacts is double-checked and correct.
The problem is that you never save the relation. You create the PFRelation within the block, add an object to it, and do nothing else with it... You never save it.
Instead, try fetching the trip object and creating the PFRelation outside of the save block, ex:
PFQuery *query = [PFQuery queryWithClassName:#"Trip"];
PFObject *trip = [query getObjectWithId:self.parseID];
PFObject *newContact = [PFObject objectWithClassName:#"Contact"];
PFRelation *rel = [trip relationForKey:#"contacts"];
[rel addObject:newContact];
[newContact saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
contact.parseID = newContact.objectId;
}];
I'm trying to query three columns that I created in my user class with parse. I successfully saved them to the user class. But I'm having difficulty query these three columns onto a label. I really don't know where to start with the query, I want it to be user specific of course, so user A gets his name, bio, gender, ect. And name is the user's full name, not their username.
This is my code to save the objects:
- (IBAction)save:(id)sender {
PFUser *profile = [PFUser currentUser];
[profile setObject:_name.text forKey:#"Name"];
[profile setObject:_bio.text forKey:#"Bio"];
NSString *var = [[NSUserDefaults standardUserDefaults] objectForKey:#"MyKey"];
[profile setObject:var forKey:#"Gender"];
[profile saveInBackground];
}
For the query I really don't know, this is where I need help. So if you have any suggestions I would appreciate it. Thank you!
Try this one
PFQuery * query = [PFQuery queryWithClassName:#"ClassName"];
[query whereKey:#"Name" equalTo:#"XYZ"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// objects : PFObjects
// you can filter out the data you want and print into lable
}];
I have a problem with incrementKey. I'd like to increment two keys of my user class, and they're not incrementing. Here's my code, if you want more details feel free to ask.
- (void) updateUserNbrQuestionsAnswered: (NSString*)FacebookID;
{
NSLog(#"%#", FacebookID);
PFQuery *queryUser = [PFQuery queryWithClassName:#"User"];
[queryUser whereKey:#"FacebookID" equalTo:FacebookID];
PFObject *user = [queryUser getFirstObject];
[user incrementKey:#"NbrQuestionsAnswered" byAmount:[NSNumber numberWithInt:1]];
[user incrementKey:#"Points" byAmount:[NSNumber numberWithInt:5]];
[user save];
};
I call this function every time the user hits a button, which reload the view after incrementing on parse and updating a label. Thanks in advance for your help !
Use this:
PFQuery *queryUser = [PFUser query]
Change the save method with
PFObject* user;
...
..
.
NSError* anyErr = NULL;
[user save:&anyErr];
Ensure you have the write permission to alter the values of the User table (see ACL)