显示待处理的朋友请求

时间:2014-05-07 19:34:28

标签: ios parse-platform

我想让表格显示待处理的好友请求列表。我跟踪朋友请求状态的方式是使用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];
}

2 个答案:

答案 0 :(得分:0)

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

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,则执行INSERT(例如, :PFObject *friendRequest = [PFObject objectWithClassName:@"FriendRequest"];     )

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

希望有所帮助

答案 1 :(得分:0)

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

-(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的首选方法。