简体   繁体   English

用Parse添加用户关系的最佳方法是什么?

[英]What is the best way to add user relations with Parse?

I have a textfield where users enter a username and click add. 我有一个文本字段,用户在其中输入用户名,然后单击添加。 Is this the best way to do this?: Make query for name of user. 这是最好的方法吗?:查询用户名。 If user does not exist, give warning message. 如果用户不存在,则发出警告消息。 If user does exist, add them to relations with this: 如果用户确实存在,则使用以下命令将它们添加到关系中:

    [self.friends addObject:user];
    [friendsRelation addObject:user];

Another question is, how do I search for a user with a query and return an object? 另一个问题是,如何通过查询搜索用户并返回对象? Also, here are some variables I made in .h 另外,这是我在.h中创建的一些变量

@property (nonatomic, strong) NSArray *allUsers;
@property (nonatomic, strong) PFUser *currentUser;
@property (nonatomic, strong) NSMutableArray *friends;
@property (nonatomic, strong) PFUser *foundUser;

- (BOOL)isFriend:(PFUser *)user;

Check out the below code and you can tailor it to your more specific needs, but it generally does what you are asking for. 查看下面的代码,您可以根据自己的特定需求对其进行定制,但是通常它可以满足您的要求。 I strongly advise you to read the documentation on all the methods in the code, and check some Parse tutorials or sample code - they have this covered extensively. 我强烈建议您阅读有关代码中所有方法的文档,并查看一些Parse教程或示例代码-它们对此进行了广泛介绍。

// create and set query for a user with a specific username
PFQuery *query = [PFUser query];
[query whereKey:@"username" equalTo:@"usernameYouWantToAdd"];

// perform the query to find the user asynchronously 
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
            // the Parse error code for "no such user" is 101
            if (error.code == 101) {
                NSLog(@"No such user");
            }
        }
        else {
            // create a PFUser with the object received from the query
            PFUser *user = (PFUser *)object;
            [friendsRelation addObject:user];
            [self.friends addObject:user];
            // save the added relation in the Parse database
            [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (error) {
                    NSLog(@" %@ %@", error, [error userInfo]);
                }
            }];
        }
    }];

Note that referencing self inside the block can lead to a retain cycle and thus a memory leak. 请注意,在块内部引用self可能导致保留周期,从而导致内存泄漏。 To prevent this, you can get create a weak reference to self outside the block, where ClassOfSelf is the class of what self is, in this case most likely your view controller: 为避免这种情况,您可以在块外部创建一个对self的弱引用,其中ClassOfSelfself的类,在这种情况下,很可能是您的视图控制器:

 __weak ClassOfSelf *weakSelf = self;

And then use it to access self in the block, for example: 然后使用它来访问块中的self ,例如:

[weakSelf.friends addObject:user];

First, to add a relation I would first get the relation column from the user, addObject on that relation, then just save the user. 首先,要添加一个关系,我首先要从用户那里获得关系列,在该关系上添加addObject,然后保存用户。

Next, what kind of query are you looking for? 接下来,您要寻找哪种查询? Do you want to query for the friends of a user? 您要查询用户的朋友吗? Query the user table for users who are friends with so and so? 在用户表中查询与某某成为好友的用户? Could you elaborate? 您能详细说明一下吗?

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

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