mercredi 27 mai 2015

I can't find the second user when joining a chat (Parse)?

When I search for a chat it successfully loads the chatview controller but it fails to find another user, so its just one user in a chat. Maybe I'm referencing the second user wrongly. Not sure how to correct it though.

Code:

- (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;
    }




Aucun commentaire:

Enregistrer un commentaire