1

I am trying to create a ConversationViewController with prespecified participants. When I create the conversation and push the controller, the participants address bar doesn’t show any names. Also, if there is an existing conversation started, the error throws.

How can I push the ConversationViewController with the specified participants?

Here is the code and debugger output to help you understand where I am:

ConversationViewController *controller = [ConversationViewController conversationViewControllerWithLayerClient:SingletonCenter.layerClient];
NSError *error;
LYRConversation *conversation = [SingletonCenter.layerClient newConversationWithParticipants:[NSSet setWithArray:@[User.objectId,self.requestForView.serviceProvider.objectId]] options:nil error:&error];
NSLog(@"conversation is: %@\nerror is: %@",conversation, [error localizedDescription]);
controller.conversation = conversation;
controller.displaysAddressBar = YES;
[self.navigationController pushViewController:controller animated:YES];

conversation is: "LYRConversation:0x7faa724a7730 identifier=layer:///conversations/c8eeaf04-085b-4c11-a985-a23aeeeb5f3e databaseIdentifier=LYRDatabaseIdentifierNotDefined version=LYRVersionNotDefined isDeleted=NO streamUUID=(null) participants={( Y8Ak1U1Mbj, AdW9c2FYeN )} distinctByParticipants=YES"

conversation is: (null) error is: A conversation with a distinct participant list already exists for participants {( Y8Ak1U1Mbj, AdW9c2FYeN )}

1 Answers1

0

When a conversation with those participants exists already that error will be thrown. The way to handle that is to fetch the conversation id from the error's userinfo.

This is directly from Layerkit's example:

// Fetches all conversations between the authenticated user and the supplied participant
// For more information about Querying, check out https://developer.layer.com/docs/integration/ios#querying
if (!self.conversation) {
    NSError *error;
    // Trying creating a new distinct conversation between all 3 participants
    self.conversation = [self.layerClient newConversationWithParticipants:[NSSet setWithArray:@[ LQSParticipantUserID, LQSParticipant2UserID  ]] options:nil error:&error];
    if (!self.conversation) {
        // If a conversation already exists, use that one
        if (error.code == LYRErrorDistinctConversationExists) {
            self.conversation = error.userInfo[LYRExistingDistinctConversationKey];
            NSLog(@"Conversation already exists between participants. Using existing");
        }
    }
}
Fabio Russo
  • 412
  • 6
  • 8