Matthew S.
Matthew S.

Reputation: 721

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?

Upvotes: 5

Views: 10030

Answers (2)

Thomas Bouldin
Thomas Bouldin

Reputation: 3725

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.

Upvotes: 6

Matthew S.
Matthew S.

Reputation: 721

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];

}];

Upvotes: 6

Related Questions