简体   繁体   English

Parse.com:PFQuery从PFUser currentUser查询数据

[英]Parse.com: PFQuery querying data from PFUser currentUser

In my iOS app, I am using the Parse.com framework. 在我的iOS应用程序中,我使用的是Parse.com框架。 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. 当应用程序第一次启动或用户未登录时, PFLogInViewControllerPFSignUpViewController将显示在视图上。 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. 当用户创建他/她的帐户时,应用程序将像往常一样自动创建用户,但它也会在用户下创建另一个名为isPrivate变量,该变量调用布尔值。 My question is how do I use the PFQuery to get the value of isPrivate for the currentUser ? 我的问题是如何使用isPrivatecurrentUser获取PFQuery的值?

After playing around with PFObject, PFUser, and PFQuery, I found the answer to my question. 在玩了PFObject,PFUser和PFQuery后,我找到了问题的答案。 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 . 您必须创建PF查询PFUser类,然后您必须将查询缩小到只有currentUser username ,然后您请求isPrivate的值。

PFQuery *query= [PFUser query];

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

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

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

}];

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 您可以决定currentUser是否为私有

[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). 这适用于所有身份验证工具(PFFacebookUtils,PFTwitterUtils和PFAnonymousUtils)。 isLinkedWithUser determines whether those auth systems have credentials for a particular user. isLinkedWithUser确定这些auth系统是否具有特定用户的凭据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM