NSNoob
NSNoob

Reputation: 5608

How to update a field in User class using parse for ios?

I am developing an app by using parse for backend. In the app, I have a registration module. User enters his email and password, and I send the data to parse and set the username temporarily to temp. On the next screen, the user is prompted to select a username, and I shall update the username in the backend accordingly. Now here's the problem: the field username's value doesn't get updated. These are few ways I have tried after looking up the internet.

//sharedclass is a singleton class, i save object id of the registered user to its attribute objectid.
NSString * objectID = sharedClass.sharedInstance->objectid;
NSLog(@"%@", objectID);
PFObject *pointer = [PFObject objectWithoutDataWithClassName:@"User" objectId:objectID];
sharedClass.sharedInstance->user.username = self.userName.text;
[pointer setObject:self.userName.text forKey:@"username"];
[pointer saveInBackground];

This one says that no object found. even though i get the objectID alright. Then there is this one here:

PFQuery *query = [PFQuery queryWithClassName:@"Users"];
[query whereKey:@"objectId" equalTo:objectID];//objectId is default parse field and objectID is the string i created
[query getFirstObjectInBackgroundWithBlock:^(PFObject * users, NSError *error) {
    if (!error) {

        [users setObject:self.userName.text forKey:@"username"];


        [users saveInBackground];
        NSLog(@"Done");
    } else {
        // Did not find any UserStats for the current user
        NSLog(@"Error: %@", error);
    }
}];

This one says no results matched the query.

Then I have this one:

PFQuery *query = [PFQuery queryWithClassName:@"Users"];

[query getObjectInBackgroundWithId:objectID block:^(PFObject *username, NSError *error) {
    username[@"username"] = self.userName.text;
    [username saveInBackground];
}];

It has the same error that query has no matching results. How do I get the query right? Can someone explain why these queries aren't working? this is how I retrieve the object id when user registration is successful

//objectid is a native string and objectId is the parse default field
sharedClass.sharedInstance->objectid = sharedClass.sharedInstance->user.objectId;

Here is screenshot of data on parse to understand the structure.

enter image description here

Upvotes: 2

Views: 1900

Answers (1)

ttarik
ttarik

Reputation: 3853

According to the Parse documentation for PFUser, and this question on the Parse Q&A site, PFUser is a special type of PFObject.

Rather than using [PFQuery queryWithClassName:], you need to call [PFUser query] to retrieve a PFQuery object that can search for objects in your Users database - for example:

PFQuery *query = [PFUser query];
[query whereKey:@"objectId" equalTo:objectID];
[query getFirstObjectInBackgroundWithBlock:....

Upvotes: 2

Related Questions