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

iOS

Luke Terzich
Luke Terzich
5,193 Points

Trouble running a query in xCode with Parse.com

hello, i am trying to allow users to search for another user in a RIBBIT type app..

I am sending a user typed variable to another view controller where the query is being run.

This is my code:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [labeltest setText:userEmail];

    PFQuery *query = [PFQuery queryWithClassName:@"User"];
    [query whereKey:@"email" equalTo:userEmail];
    [query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {

        if(error)
        {
            //The Query Failed!
            NSLog(@"Query Failed");
        }
        else
        {
            //The Query Succeeded!
            NSLog(@"Successfully retrieved %d users.", comments.count);
        }

    }];

}

If anybody can shed any light as to where i am going wrong, I have a label on the view so i can see that it is getting the correct email address (that the user typed in) Also in my Xcode Log i am getting Successfully retrieved 0 users. My class in Parse.com is called "User"

Thank you to anybody that can help


UPDATE: I have just added an NSLOG to the actually query just before the IF statement is called. if i try to print the variable "userEmail" it is (null) in the Xcode Log viewer

Cannot understand why when just above it (outside the query) i am calling the variable and it is printed on the screen?

4 Answers

Soojin Ro
Soojin Ro
13,331 Points

If you wish to get a PFUser as a result of your query, you should initialize query like this,

PFQuery *query = [PFUser query];

Moreover, you can do what you want with just three lines(given that email is unique value among users)

PFQuery *query = [PFUser query];
[query whereKey:@"email" equalTo:@"email string"];
PFUser *user = (PFUser *)[query getFirstObject];
Luke Terzich
Luke Terzich
5,193 Points

Thank you so much! been looking for this for ages and it was as easy as that! Thank you pal!!!!!!

Luke Terzich
Luke Terzich
5,193 Points

Sorry I'm a little new to this, how would i save a specific field as a variable? Thank you in advanced pal

Soojin Ro
Soojin Ro
13,331 Points

what do you mean a specific field? like a textfield?

Luke Terzich
Luke Terzich
5,193 Points

like a column in the class,i want to get the users email address in the address field in the class so i can show it on the screen :)

If that makes sense?