Search code examples
androidiosquickblox

start all dialogs (chat room) to autodownload attachments in quickblox


What i want to implement is auto-download attachment functionality without entering in ChatRoom, using QuickBlox SDK iOS version 2.0.12,

To do that all i want to start all the dialogs (chat rooms) that user belongs after login.

With current api what user can see the list of Dialogs (chat rooms) and enter in only one room at a time. So, suppose there are 2 chat rooms (ChatRoom A,ChatRoom B) for the logged in user, so when he enters 'ChatRoom A', he can only receive messages/attachments for 'ChatRoom A' as Notification in chatDidReceiveMessageNotification / chatRoomDidReceiveMessageNotification.

So when user receives a message/attachment for 'ChatRoom B' he will not be able to access until he enters the 'ChatRoom B'.

To have it worked, i created following method in DialogsViewController : which is trying to join all the dialogs(chat rooms)

-(void)startallrooms
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chatDidReceiveMessageNotification:)
                                                     name:kNotificationDidReceiveNewMessage object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chatRoomDidReceiveMessageNotification:)
                                                     name:kNotificationDidReceiveNewMessageFromRoom object:nil];
    QBChatRoom *chatRooms;

    for (int i=0; i<=self.dialogs.count; i++) {
        QBChatDialog *dialog = self.dialogs[i];
        chatRooms = [dialog chatRoom];
        [[ChatService instance] joinRoom:chatRooms completionBlock:^(QBChatRoom *joinedChatRoom) {
                // joined
        }];
    }   
}

And called from completedWithResult from DialogsViewController.

- (void)completedWithResult:(Result *)result{
    if (result.success && [result isKindOfClass:[QBDialogsPagedResult class]]) {
        [self startallrooms];
        ..
        ..
     }
}

It is giving an error : EXC_BAD_ACCESS in following method from ChatService.m

- (void)chatRoomDidEnter:(QBChatRoom *)room{
}

But when i try to join only one ChatRoom, it doesn't give me any error and works perfectly fine. (just check replacing for loop with this -> for (int i=0; i<=0; i++) in startallrooms method that i mentioned above)

So, where am i going wrong and even if i solve this issue, is it feasible from memory perspective to start all the rooms (if user is having thousands of rooms) ? After implementing same i will be doing it on Android also.


Solution

  • If you look inside the ChatService class, you may see that after success -chatRoomDidEnter, the completion block will be released. It means that the second call of this method will cause EXC_BAD_ACCESS. Just don't release block.