2

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

9
  • What is objectID when you first print it out? I just tested out some of your code with a Parse app of mine and it works perfectly, so the problem must be the object ID. Commented Oct 24, 2014 at 7:50
  • The NSLog gives kmdayobb9, objectID of the object created at that test. That'd be the object with username "nrkd" in the screenshot (i updated it manually from parse web). That's what's baffling. If objectID is correct why is query not matching any results? here is the console output to give you more insight. i61.tinypic.com/33z8dba.png Commented Oct 24, 2014 at 8:00
  • Your screenshot shows an object with an ID Okmdayobb9 (notice the 'O' at the beginning) - was that a typo in your comment or in your code? Is the objectID in your app hard-coded or does it retrieve it from somewhere? Commented Oct 24, 2014 at 8:03
  • I edited the question with retrieval method for objectId. and it was a typo in my comment sorry about that Commented Oct 24, 2014 at 8:07
  • 1
    [PFUser query] returns a PFQuery object, e.g. PFQuery *query = [PFUser query];. It's the recommended way of querying PFUser objects. Docs: parse.com/docs/ios/api/Classes/PFUser.html#//api/name/query Commented Oct 24, 2014 at 8:20

1 Answer 1

2

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:....
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.