Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Sean Lee
5,196 PointsParse Stuck On Updating Data
I hope someone on this forum is able to help me out. I've been stuck on this for the past few days and I've been at it non-stop. Basically what I'm trying to do is update Objects in the 'User' class that was created when the user creates an account. So when an account is created, the user submits the username, password, email and I've created additional objects (columns) in parse to later update name, address, city, state, zip, phone#.
in my "displayProfile" method, I'm just calling data from parse of the current user's information which is getting displayed (I manually inputted data in the Parse database). The fields are getting populated.
I have a button "updateProfile" when it is pressed an action is called to update the currentUser's information in the textfields. I am having trouble running the Parse code to update the class default user class 'User'. Is that class even editable?
I've been looking all over the internet, stack overflow and I could not find any additional information that provided insights to my problem.
thanks in advance!
- (void)displayProfile {
self.nameField.text = [[PFUser currentUser] objectForKey:@"name"];
self.phoneField.text = [[PFUser currentUser] objectForKey:@"phone"];
self.cityField.text = [[PFUser currentUser] objectForKey:@"city"];
self.stateField.text = [[PFUser currentUser] objectForKey:@"state"];
self.streetField.text = [[PFUser currentUser] objectForKey:@"street"];
self.zipField.text = [[PFUser currentUser] objectForKey:@"zip"];
self.emailField.text = [[PFUser currentUser] objectForKey:@"email"];
}
- (void)updateProfile {
NSString *name = [self.nameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *phone = [self.phoneField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *street = [self.streetField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *city = [self.cityField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *state = [self.stateField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *zip = [self.zipField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *email = [self.emailField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
PFUser *currentUser = [PFUser currentUser];
NSString *string = currentUser.objectId;
PFQuery *query = [PFQuery queryWithClassName:@"User"];
[query getObjectInBackgroundWithId:string block:^(PFObject *object, NSError *error) {
object[@"name"] = name;
object[@"phone"] = phone;
object[@"street"] = street;
object[@"city"] = city;
object[@"state"] = state;
object[@"zip"] = zip;
object[@"email"] = email;
[object saveInBackground];
}];
}
4 Answers
Stone Preston
42,016 Pointsyou dont need to run a query in your updateProfile method. all you need to do is update the fields of your currentUser then save the user object
- (void)updateProfile {
NSString *name = [self.nameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *phone = [self.phoneField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *street = [self.streetField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *city = [self.cityField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *state = [self.stateField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *zip = [self.zipField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *email = [self.emailField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
PFUser *currentUser = [PFUser currentUser];
currentUser[@"name"] = name;
currentUser[@"phone"] = phone;
currentUser[@"street"] = street;
currentUser[@"city"] = city;
currentUser[@"state"] = state;
currentUser[@"zip"] = zip;
currentUser[@"email"] = email;
[currentUser saveInBackground];
}
Sean Lee
5,196 PointsPreston,
Thank you SO much! I know it probably one of the easiest problems to solve but it relieved a lot of built up frustration. The Parse guides are not thorough and for a beginner like myself there a bit of learning curve.
I have to say that from my experience TH has a great community and support. Thanks for your contributions b/c this isn't the first time you've helped me out.
Sean
Stone Preston
42,016 Pointsno problem : )
Sean Lee
5,196 Pointshi Preston,
thanks for your help, I have another question regarding saving to Parse backend.
Just to paint a better picture: 1) FirstViewController takes in name, address, city, state, zip email, phone from the user. 2) User clicks button Next to bring them to the SecondViewController. 3) SecondViewController takes in additional information such as friend's name, address, city, state, zip, email, phone 4) ThirdViewController takes in a picture 5) Now I want to click "SAVE" and have all the inputs save in Parse back end in the same row.
Lets say that in the First View Controller I create a new Class in the Parse backend and save that data. I want to implement a NEXT button to go to the Second and Third View Controller that collects more objects from the user to save within the same Class in the First View Controller. How would I go about doing that? Would I have to save every time I press NEXT? But then you might end up with useless data in the backend if the user quits in the middle of using the app.
Is there a way to temporarily save the objects as users scroll through multiple view controllers and upload/create the row of objects all at once in the end?
thanks!
Stone Preston
42,016 Pointsyou could pass along the data in prepareForSegue to each view controller, then your last view controller would have everything it needed.
Sean Lee
5,196 PointsHi Preston,
Appreciate the guidance! I was able to pass data from the first view controller to the second. Now i'm working on passing it back to the first view controller using a delegate/protocol but I have come to a road block.
I am able to pass it to child “StepTwoViewController” but can not pass it back to parent “StepOneViewController” If you have some time can you let me know what's wrong with my code? For some reason my <StepTwoViewControllerDelegate> is giving me a error.
Git: https://github.com/aspirasean/SnapShip/tree/master/SnapShip
thanks!