1

I'm trying to remove dialog object in QMServicesManager, So when I want to delete the dialog I'm doing the below

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [ServicesManager.instance.chatService.dialogsMemoryStorage dialogsSortByUpdatedAtWithAscending:NO].count;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    QBChatDialog *dialog = [self.modelArray objectAtIndex:indexPath.row];


    if (dialog.type == QBChatDialogTypePrivate) {

    [QBRequest deleteDialogsWithIDs:[NSSet setWithObject:dialog.ID] forAllUsers:NO
                       successBlock:^(QBResponse *response, NSArray *deletedObjectsIDs, NSArray *notFoundObjectsIDs, NSArray *wrongPermissionsObjectsIDs) {
                           NSLog(@"FJFFJFJ");

//                           [ServicesManager.instance.chatService.messagesMemoryStorage deleteMessagesWithDialogID:dialog.ID];
                           [ServicesManager.instance.chatService.dialogsMemoryStorage deleteChatDialogWithID:dialog.ID];
                           [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

                       } errorBlock:^(QBResponse *response) {

                           NSLog(@"GFGFGFGFG");
                       }];
    }
}

So that whenever i say delete I'm calling deleteDialogsWitIDs api. So that I'm getting success response. If I get success response, then only I'm removing the dialog from my table view dialog list. As written above.

Here the problem is when ever I remove dialog from ServicesManager it is removing in the dialogsMemoryStorage so the count is decreasing(Example initial count is 10 after deleting it is showing count as 9 and its reloading table view success fully).

But when i quit the app and then re launch the application it is not removing the deleted dialog inside the memory(i.e, it is showing actual count as 10 but not as 9). So expected behaviour is, It should give new count(9) but not the old count(10).

What I understood is, it is deleting temporarily for the session but not deleting in the DB i guess. Or else Am I doing anything wrong ? How to achieve this ?

Updated Question : After some trial and errors I got the solution I'm not doing all those stuff inside comitEditingStyle:, I'm simply calling deleteDialogWithID: it is handling everything. The code is modified this

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

        QBChatDialog *dialog = [self.modelArray objectAtIndex:indexPath.row];


        if (dialog.type == QBChatDialogTypePrivate) {

            [ServicesManager.instance.chatService deleteDialogWithID:dialog.ID completion:nil];   

    }
}

But I got new problem :

I logged in as User1 and I created chat with User2 and User3 separately (2 different private chats) and then I chatted also. Then I deleted dialog with User2 Now I have Only User3's Dialog in my dialog list.

But If I want to create NewDialog With User2 then It is showing my old latest message in my newly created dialog with user2. But What I want is, It should create a new dialog with empty latest message ? (New Dialog with User 2) I hope I'm clear.. How to do this ?

Question Updated With New Requirement :

Now If I want to delete group chat how I should handle this ? If I use the same method inside it we are passing forAllUsers as "NO" which is hard coded. written inside QMChatServices.m

- (void)deleteDialogWithID:(NSString *)dialogId completion:(void (^)(QBResponse *))completion {

    NSParameterAssert(dialogId);

    __weak __typeof(self)weakSelf = self;

    [QBRequest deleteDialogsWithIDs:[NSSet setWithObject:dialogId] forAllUsers:NO successBlock:^(QBResponse *response, NSArray *deletedObjectsIDs, NSArray *notFoundObjectsIDs, NSArray *wrongPermissionsObjectsIDs) {
        //
        [weakSelf.dialogsMemoryStorage deleteChatDialogWithID:dialogId];
        [weakSelf.messagesMemoryStorage deleteMessagesWithDialogID:dialogId];

        if ([weakSelf.multicastDelegate respondsToSelector:@selector(chatService:didDeleteChatDialogWithIDFromMemoryStorage:)]) {
            [weakSelf.multicastDelegate chatService:weakSelf didDeleteChatDialogWithIDFromMemoryStorage:dialogId];
        }

        [weakSelf.loadedAllMessages removeObjectsForKeys:deletedObjectsIDs];

        if (completion) {
            completion(response);
        }
    } errorBlock:^(QBResponse *response) {
        //
        if (response.status == QBResponseStatusCodeNotFound || response.status == 403) {
            [weakSelf.dialogsMemoryStorage deleteChatDialogWithID:dialogId];

            if ([weakSelf.multicastDelegate respondsToSelector:@selector(chatService:didDeleteChatDialogWithIDFromMemoryStorage:)]) {
                [weakSelf.multicastDelegate chatService:weakSelf didDeleteChatDialogWithIDFromMemoryStorage:dialogId];
            }
        }
        else {

            [weakSelf.serviceManager handleErrorResponse:response];
        }

        if (completion) {
            completion(response);
        }
    }];
}

So Now my doubt is..

Question 1 : What if we want to delete dialog for all the users. Question 2 : Lets say there are 3 users. User1 , User2 and User3. Now User1 has created group with User2 and User3.

So how this method is useful for all the different 3 users. I mean What happens if User1 uses

[ServicesManager.instance.chatService deleteDialogWithID:dialog.ID completion:nil];

and what happens if User2 and User3 uses the same method.

Weather it works as exit from the dialog or deleting dialog. I'm little confused how this method works for different users in case of group and public chat.

Question 3 : Is there any other way to exit from the group chat ? I hope it is clear !!

1 Answers1

2

Why aren't you using the power of QMServices?)

You can simply delete the dialog with the following method:

 // Deleting dialog from Quickblox and cache.
    [ServicesManager.instance.chatService deleteDialogWithID:dialogID
                                                  completion:^(QBResponse *response) {
                                                        if (response.success) {
                                                            __typeof(self) strongSelf = weakSelf;
                                                            [strongSelf.tableView reloadData];

                                                        } else {

                                                            NSLog(@"can not delete dialog: %@", response.error);
                                                        }
                                                    }];
VitGur
  • 539
  • 9
  • 13
  • Yeah now I"m doing this only. I updated my question. Please check it. And there I got one more question. – Nithin Mangolu May 11 '16 at 15:13
  • Now with your answer I confirmed that I'm doing in a correct way. But there I have one more problem. If I want to create a new dialog with the same user. after deleting the dialog. It is showing me the last message in the newly created dialog. how to handle this ? – Nithin Mangolu May 11 '16 at 15:16
  • @Wills-In As for your second question it's a server-side issue. We get last message from the server. We are going to fix this issue in the newest updates. Please follow [this link](https://quickblox.com/blog/) for updates. – VitGur May 11 '16 at 15:33
  • @Wills-In, If you still have questions please create an issue [in our repo](https://github.com/QuickBlox/quickblox-ios-sdk). – VitGur Feb 20 '17 at 12:06