user3355866
user3355866

Reputation: 13

Retrieving row of data from parse

I'm new in using parse

I know what I'm going to ask about is very basic, but please go easy on me

in my program, when a person log-in, I should be able to retrieve his data and display them on labels I tried to use this code, but Ii get nothings in them:

PFQuery *query = [PFQuery queryWithClassName:@"User"];
[query getObjectInBackgroundWithId:[PFUser currentUser].objectId block:^(PFObject *user, NSError *error) {
    _name.text = user[@"usrename"];
    _age.text = [NSString stringWithFormat:@"%@",user[@"age"]];
    _phone.text = [NSString stringWithFormat:@"%@",user[@"phone"]];

and also I get this msg:

2014-03-15 17:16:41.226 abc[7005:60b] Error: no results matched the query (Code: 101, Version: 1.2.18) 2014-03-15 17:16:41.226 abc[7005:60b] (null)

Upvotes: 0

Views: 255

Answers (1)

Logan
Logan

Reputation: 53142

You shouldn't have to do a query after login, the user is right there:

[PFUser logInWithUsernameInBackground:@"userName" password:@"secretPassword" block:^(PFUser *user, NSError *error) {
    _name.text = user.username;
    _age.text = [NSString stringWithFormat:@"%@",user[@"age"]];
    _phone.text = [NSString stringWithFormat:@"%@",user[@"phone"]];
}];

If you need to access it anywhere else in your app, parse created a singleton for you:

NSLog(@"The Current User Is: %@", [PFUser currentUser]);

Other notes:

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

Should Be:

PFQuery * userQuery = [PFUser query]; // for user queries

You're using the current user object to get the current user in the background with this:

[query getObjectInBackgroundWithId:[PFUser currentUser].objectId block:^(PFObject *user, NSError *error) {
    _name.text = user[@"usrename"];
    _age.text = [NSString stringWithFormat:@"%@",user[@"age"]];
    _phone.text = [NSString stringWithFormat:@"%@",user[@"phone"]];
}];

If you already have access to the current user object, you can update it like this to get the latest info from parse

[[PFUser currentUser] fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if (!error) {
        // user is fully loaded
    }
}];

Upvotes: 2

Related Questions