2

I have create an application for chatting. I have use QuickBlox to chat with each other.

I have register two user and chat with each other it will work perfect. But when user logout and login again it will crash.

ERROR - Dialog have to be in memory cache!

EX: A and B user login with their device and chat with each other after that when they logout and login again and when they send message application crash.

LOGIN

- (void)loginWithQuickBlox:(NSString *)idandPassword {
    [QBRequest logInWithUserLogin:idandPassword password:idandPassword successBlock:^(QBResponse *response, QBUUser *user)
     {
        // NSLog(@"User Id : %ld",(unsigned long)user.ID);

         NSString *usrID=[NSString stringWithFormat:@"%ld",(unsigned long)user.ID];
         [[NSUserDefaults standardUserDefaults]setObject:usrID forKey:@"LoginQuickbloxID"];

         [self loginWithQuickBloxChat:idandPassword];
     } errorBlock:^(QBResponse *response)
     {
         // error handling
         NSLog(@"error: %@", response.error);
     }];
}

- (void)loginWithQuickBloxChat:(NSString *)idandPassword {

    QBUUser *selectedUser = [QBUUser user];
    selectedUser.password = idandPassword;
    selectedUser.login = idandPassword;

    [ServicesManager.instance logInWithUser:selectedUser completion:^(BOOL success, NSString *errorMessage)
     {
         if (success)
         {
             [self getRecenetChatUsingInBadgeCount];
             NSLog(@"Login in Quickblox");
             [[NSUserDefaults standardUserDefaults]setObject:idandPassword forKey:@"QuickbloxIDPass"];
         }
         else
         {
             NSLog(@"Error in QuickBlox");
         }
     }];
}

CHATTING

Chatting with ChatViewController.

LOGOUT

1)Unsubscribed Device Token

NSString *deviceUdid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
                [QBRequest unregisterSubscriptionForUniqueDeviceIdentifier:deviceUdid successBlock:^(QBResponse *response) {
                    // Unsubscribed successfully
                       NSLog(@"Unsubscribed successfully");

                } errorBlock:^(QBError *error) {
                    // Handle error
                    NSLog(@"Unsubscribed ERROR");
                }];

2)ServicesManager Logout

[ServicesManager.instance logoutWithCompletion:^{

           NSLog(@"logoutWithCompletion");

    }];

3)logOut With Success Block

[QBRequest logOutWithSuccessBlock:^(QBResponse *response) {
           // Successful logout
              NSLog(@"Successful logout");

     } errorBlock:^(QBResponse *response) {
              // Handle error
               NSLog(@"Logout ERROR %@",response);
   }];

When Message send after logout and login again crash here

QMChatService.m

- (void)sendMessage:(QBChatMessage *)message
         toDialogID:(NSString *)dialogID
      saveToHistory:(BOOL)saveToHistory
      saveToStorage:(BOOL)saveToStorage
         completion:(QBChatCompletionBlock)completion
{
    NSCParameterAssert(dialogID);
    QBChatDialog *dialog = [self.dialogsMemoryStorage chatDialogWithID:dialogID];
    NSAssert(dialog != nil, @"Dialog have to be in memory cache!");

    [self sendMessage:message toDialog:dialog saveToHistory:saveToHistory saveToStorage:saveToStorage completion:completion];
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
sohil
  • 818
  • 2
  • 15
  • 38

1 Answers1

1

The answer is late, but maybe someone will find this helpful.
In order for the dialogue to be in memory, it must be loaded with the correct method (some methods doesn't do this action).
Here is how i do that (but on the swift, i'm sorry)

private func sendMessage(message: QBChatMessage) {
    if ServicesManager.instance().chatService.dialogsMemoryStorage.chatDialog(withID: self.dialog.id!) != nil {
        self.send(message: message)
    } else {
        loadAndSaveToMemoryDialog(message)
    }
}

private func send(message: QBChatMessage) {
    ServicesManager.instance().chatService.send(message, toDialogID: self.dialog.id!, saveToHistory: true, saveToStorage: true) { (error in
        if error != nil {
            QMMessageNotificationManager.showNotification(withTitle: "SA_STR_ERROR", subtitle: error?.localizedDescription, type: .error, handler: nil)
        }
    }
}

private func loadAndSaveToMemoryDialog(_ message: QBChatMessage) {
    ServicesManager.instance().chatService.loadDialog(withID: self.dialog.id!) {
        [weak self] _ in
        guard let self = self else { return }
        self.sendMessage(message: message)
    }
}
Koder 228
  • 200
  • 1
  • 8