Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse.com: PFQuery querying data from PFUser currentUser

In my iOS app, I am using the Parse.com framework. When the app is launched for the first time or the user isn't signed in, the PFLogInViewController and the PFSignUpViewController will show up on the view. When the user creates his/her account, the application will automatically create the user, like always, but it will also create another variable under the user which is called isPrivate, which calls for a boolean. My question is how do I use the PFQuery to get the value of isPrivate for the currentUser?

like image 268
Matthew S. Avatar asked Dec 01 '12 20:12

Matthew S.


2 Answers

Querying helps you find many objects matching a criteria. This doesn't really apply when you already know the object you want (the current user). You can decide whether the currentUser is private with

[PFUser.currentUser[@"isPrivate"] boolValue];

Though in this particular case, you don't actually need to keep a separate field to determine whether the user is an anonymous user (the type created by automatic users). Try:

[PFAnonymousUtils isLinkedWithUser:PFUser.currentUser];

This works for all authentication utils (PFFacebookUtils, PFTwitterUtils, and PFAnonymousUtils). isLinkedWithUser determines whether those auth systems have credentials for a particular user.

like image 61
Thomas Bouldin Avatar answered Oct 10 '22 04:10

Thomas Bouldin


After playing around with PFObject, PFUser, and PFQuery, I found the answer to my question. You have to create a PFQuery the queries the PFUser class, then you have to narrow the query down to only the currentUser username, then you request the value of isPrivate.

PFQuery *query= [PFUser query];

[query whereKey:@"username" equalTo:[[PFUser currentUser]username]];

[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error){

    BOOL isPrivate = [[object objectForKey:@"isPrivate"]boolValue];

}];
like image 34
Matthew S. Avatar answered Oct 10 '22 03:10

Matthew S.