如何在Parse上实现随机聊天功能?

时间:2015-05-25 14:32:58

标签: objective-c random parse-platform chat

我已经使用Parse构建了帐户后端,但我不知道如何搜索并加入与其他用户的随机聊天。当我点击聊天按钮时,我希望它能够搜索并加入聊天。我应该怎么看才能实现这个目标?谢谢!

编辑:几乎就在那里,无法搜索/找到其他用户。

    //-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)actionChat:(NSString *)groupId
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
    ChatView *chatView = [[ChatView alloc] initWith:groupId];
    // chatView.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:chatView animated:YES]; // This JSQMessageViewController
}


- (IBAction)startChat:(id)sender { // The button

    PFQuery *query = [PFUser query];

    [query whereKey:@"objectId" notEqualTo:[PFUser currentUser].objectId];
    [query setSkip:arc4random()%2];
    [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
        if (!object) {
            NSLog(@"The getFirstObject request failed.");

        } else {
            //You now have a random user from your Database, do what you want with it.

                PFUser *user1 = [PFUser currentUser];

                NSString *groupId = StartPrivateChat(user1,object);
                [self actionChat:groupId];

        }
    }];
}




//-------------------------------------------------------------------------------------------------------------------------------------------------
    NSString* StartPrivateChat(PFUser *user1, PFUser *user2)
    //-------------------------------------------------------------------------------------------------------------------------------------------------
    {
        NSString *id1 = user1.objectId;
        NSString *id2 = user2.objectId;
        //---------------------------------------------------------------------------------------------------------------------------------------------
        NSString *groupId = ([id1 compare:id2] < 0) ? [NSString stringWithFormat:@"%@%@", id1, id2] : [NSString stringWithFormat:@"%@%@", id2, id1];
        //---------------------------------------------------------------------------------------------------------------------------------------------
        NSArray *members = @[user1.objectId, user2.objectId];
        //---------------------------------------------------------------------------------------------------------------------------------------------
        // CreateRecentItem(user1, groupId, members, user2[PF_USER_FULLNAME]);
        // CreateRecentItem(user2, groupId, members, user1[PF_USER_FULLNAME]);
        //---------------------------------------------------------------------------------------------------------------------------------------------
        return groupId;
    }

1 个答案:

答案 0 :(得分:1)

 PFQuery *query = [PFUser query];

 [query whereKey:@"objectId" notEqualTo:[PFUser currentUser].objectId];
 [query setSkip:arc4random()%YOUR_TOTAL_USERS];
 [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
            if (!object) {
                NSLog(@"The getFirstObject request failed.");

            } else {
                //You now have a random user from your Database, do what you want with it.
            }
        }];
相关问题