【问题标题】:Showing pending friend requests显示待处理的好友请求
【发布时间】:2023-03-21 08:34:01
【问题描述】:

我想要一个表格来显示待处理的好友请求列表。我跟踪好友请求状态的方法是使用 Parse 作为后端并创建一个 FriendRequest 类。在该类中,有一个状态列已接受、待处理、拒绝。这是当前的查询逻辑。

 PFQuery *userQuary = [PFUser query];
    PFQuery *pendingFriends = [PFQuery queryWithClassName:@"FriendRequest"];
    PFQuery *aceeptedFriends = [PFQuery queryWithClassName:@"FriendRequest"];
    [aceeptedFriends whereKey:@"toUser" equalTo:self.currentUser.objectId];
    [aceeptedFriends whereKey:@"status" equalTo:@"Accepted"];
    [pendingFriends whereKey:@"status" doesNotMatchKey:@"status" inQuery:aceeptedFriends];
    [pendingFriends whereKey:@"toUser" equalTo:self.currentUser.objectId];
    [pendingFriends whereKey:@"status" equalTo:@"Pending"];
    [userQuary whereKey:@"objectId" matchesKey:@"fromUser" inQuery:pendingFriends];
    [userQuary findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error){
            NSLog(@"ERROR: %@ %@", error, [error userInfo]);
        }else{
            self.friendList = objects;
            NSLog(@"%@", self.friendList);
            [self.tableView reloadData];
        }
    }];

我遇到的问题是已经被接受的朋友不断出现在待定朋友的查询中。

这里是作为接受好友请求的方法。 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    PFRelation *myRelation = [self.currentUser relationForKey:@"friendRelation"];
    //PFRelation *friendsRelation = [self.selectedUser relationForKey:@"friendRelation"];
    PFObject *friendRequest = [PFObject objectWithClassName:@"FriendRequest" ];
    [friendRequest setObject:self.currentUser.objectId forKey:@"fromUser"];
    [friendRequest setObject:self.selectedUser.objectId forKey:@"toUser"];
    [friendRequest setObject:@"Accepted" forKey:@"status"];

    switch (buttonIndex) {
        case 0:
            [friendRequest saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (error){
                    NSLog(@"ERROR: %@, %@", error, [error userInfo]);

                }
            }];
            [myRelation addObject:self.selectedUser];
            [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (error){
                    NSLog(@"ERROR: %@ %@", error, [error userInfo]);

                }
            }];

        case 2:
            [alertView dismissWithClickedButtonIndex:1 animated:0];
            break;
    }
    /*
    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error){
            NSLog(@"ERROR: %@ %@", error, [error userInfo]);
        }
    }];
     */
    [self.tableView reloadData];
}

【问题讨论】:

    标签: ios parse-platform


    【解决方案1】:

    如果您想要待处理的好友请求,您应该执行以下操作:

    PFQuery *userQuery = [PFUser query];
    PFQuery *pendingFriends = [PFQuery queryWithClassName:@"FriendRequest"];
    
    // Supposing the user at which ask the friend request
    [pendingFriends whereKey:@"toUser" equalTo:self.currentUser.objectId];
    
    // Only pending state flag
    [pendingFriends whereKey:@"status" equalTo:@"Pending"];
    
    // get all users that have asked a friend request to current user
    [userQuery whereKey:@"objectId" matchesKey:@"fromUser" inQuery:pendingFriends];
    
    // run the inner query
    [userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error){
            NSLog(@"ERROR: %@ %@", error, [error userInfo]);
        }else{
            self.pendingUserRequests = objects;            
            [self.tableView reloadData];
        }
    }];
    
    
    ...
    ..
    .
    
    -(void) acceptUserRequestAtIndex:(NSUInteger)pendingUserIndex
    {
        // This is the currently existing user that want friendship
        // Note that my tableview datasource is the same of code above, so the previously loaded list of pending friend request users
        PFUser* selectedPendingUser = [self.pendingUserRequest objectAtIndex:pendingUserIndex];
    
        // Composing the query for obtain CURRENT EXISTING friend request between current user and applicant pending user
        PFQuery *friendRequestQuery = [PFQuery queryWithClassName:@"FriendRequest"];
        [friendRequestQuery whereKey:@"toUser" equalTo:self.currentUser.objectId];
        [friendRequestQuery whereKey:@"fromUser" equalTo:selectedPendingUser.objectId];     
        [friendRequestQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (error){
                NSLog(@"ERROR: %@ %@", error, [error userInfo]);
            }else{
    
                if ([objects count] == 0){
                    NSLog(@"You can't accept a non existing friend request!");
                    return;
                } 
    
                // IMPORTANT: I'm assuming that you could find ONLY one request with the same toUser and fromUser combination!! So in your function in which you make a new request, you should check that another request with same toUser and fromUser already exist, and so avoid to make new one
                PFObject* requestRowObj = [objects objectAtIndex:0]; 
                [requestRowObj setObject:@"Accepted" ForKey:@"status"]; // change the status request between current user and applicant pending user to Accepted
    
                // Update the existing object on server with new modification ( so the only status field )
                [requestRowObj  saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)   {
                    if (error){
                        NSLog(@"ERROR: %@, %@", error, [error userInfo]);
                    }
                }];
        }];
    }
    

    这样,您将避免再次选择现在已接受的好友请求,因为它们已被标记为已接受。请记住,如果您调用 PFObject 的 saveInBackgroundWithBlock 方法(PFUser 也是 PFObject 的子类),如果该对象已从先前的查询操作中选择,您将执行 UPDATE,如果您创建一个新的 PFObject(例如:PFObject *friendRequest = [PFObject objectWithClassName:@"FriendRequest" ]; )

    (注意:如果您认为此查询可能有超过 1000 个结果,请不要忘记使用跳过和限制属性对该查询进行分页,根据解析选择限制)

    希望对你有帮助

    【讨论】:

    • 我遇到的问题是,如果用户接受好友请求,则状态设置为已接受。但是,待处理的旧状态仍保留在单独的行中,我不希望经常看到在我的应用中不再有效的旧待处理请求。
    • 一旦您接受请求,您就知道刚刚接受的用户的对象 ID,并且您知道当前的用户对象 ID,因此只需进入 FriendRequest 类并选择带有“”的特定行toUser”和那个“fromUser”,并将他的状态字段更新为接受。因此,使用 PFObject 方法 saveInBackgroundWithBlock 在后台保存。这样,下次用户请求将不再出现在您的待处理请求中
    • 您能详细介绍一下吗?简单地使用相同的键设置 fromUser 和 toUser 不会导致替换,它会在 FriendRequest 表中创建一个新行。我还更新了上面的代码以包含更改状态的方法,但目前它在表中创建新行而不是更新它。
    • 正如我在上面所读到的,您正在创建一个新的 PFObject,然后在后台保存。这将创建一个新的行服务器端。相反,您应该查询 FriendRequest 表并检索具有 fromUser 和 toUser 具有正确值的当前现有行。我将更新 mi 代码以更清晰
    【解决方案2】:

    这是我在同一行中更新状态的解决方案。

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        self.selectedUser = [self.friendList objectAtIndex:indexPath.row];
        NSString *requestString = [NSString stringWithFormat:@"%@ wants to add you as a connection", self.selectedUser.username];
        UIAlertView *confirmationAlert = [[UIAlertView alloc]initWithTitle:@"Pending Connection Request"  message:requestString delegate:self cancelButtonTitle:nil  otherButtonTitles:@"Accept", @"Cancel", nil];
        [confirmationAlert show];
    
    }
    

    我们已经将 self.selectedUser 设置为一个属性,它现在可以用作数据源来将我们需要的信息传递给另一个方法。

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    
        PFQuery *rowIdQuery = [PFQuery queryWithClassName:@"FriendRequest"];
        [rowIdQuery whereKey:@"toUser" equalTo:self.currentUser.objectId];
        [rowIdQuery whereKey:@"fromUser" equalTo:self.selectedUser.objectId];
        [rowIdQuery whereKeyExists:@"objectId"];
    
    
        switch (buttonIndex) {
            case 0:
                [rowIdQuery getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
                    if(error){
                        NSLog(@" ERROR: %@ %@", error, [error userInfo]);
                    }else{
                        [object setObject:@"Accepted" forKey:@"status"];
                        [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                            if (error){
                                NSLog(@"ERROR: %@ %@", error, [error userInfo]);
                            }else{
                                NSLog(@"Save Happoned");
                            }
                        }];
                    }
                }];
    
    
               case 2:
                [alertView dismissWithClickedButtonIndex:1 animated:0];
                break;
    }
    

    查询可以获取对象,这些对象可以设置 setKey forKey 方法。根据 Parse API,这是首选方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 2017-10-03
      • 2018-05-28
      • 2013-05-01
      • 2018-04-14
      • 1970-01-01
      相关资源
      最近更新 更多