ios - Quickblox dialog doesn't update when receiving new message -


whenever send or receive message in dialog (they of type public_group) won't updated until dismiss dialogviewcontroller , open again. code:

static nsstring *chatmessagecellidentifier = @"chatmessagecellidentifier";  @interface dialogviewcontroller () <chatservicedelegate,qbchatdelegate, qmchatservicedelegate, uitableviewdelegate, uitableviewdatasource>  @property (nonatomic, weak) iboutlet uitextfield *messagetextfield; @property (nonatomic, weak) iboutlet uibutton *sendmessagebutton; @property (nonatomic, weak) iboutlet uiview *inputbackgroundview; @property (nonatomic, weak) iboutlet uitableview *tableview;  @property (nonatomic, assign) bool iskeyboradshow;  @property (nonatomic, strong) uirefreshcontrol *refreshcontrol;  @property (nonatomic, strong) nsmutablearray *messagearray;  - (ibaction)sendmessage:(id)sender;  @end  @implementation dialogviewcontroller  - (void)viewdidload {     [super viewdidload];      self.messagearray = [nsmutablearray array];      [[qbchat instance] adddelegate:self];      [[qbchat instance] setautoreconnectenabled:yes];      self.tableview.separatorstyle = uitableviewcellseparatorstylenone;      uitapgesturerecognizer *gesturerecognizer = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(hidekeyboard)];     [self.view addgesturerecognizer:gesturerecognizer];      // initialize refresh control.     self.refreshcontrol = [[uirefreshcontrol alloc] init];     self.refreshcontrol.backgroundcolor = [uicolor whitecolor];     [self.refreshcontrol addtarget:self                             action:@selector(getpreviousmessages)                   forcontrolevents:uicontroleventvaluechanged];      [self.tableview addsubview:self.refreshcontrol];      //gesture  /*   uitapgesturerecognizer *tapgesture = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(messagetextfielddidtap:)];     [self.messagetextfield addgesturerecognizer:tapgesture];   */       //table view cells     [self.tableview registerclass:[chatmessagetableviewcell class] forcellreuseidentifier:chatmessagecellidentifier]; }  - (void)viewwillappear:(bool)animated{     [super viewwillappear:animated];      nslog(@"chat: %@", self.dialog);      // set keyboard notifications     [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillshow:)                                                  name:uikeyboardwillshownotification object:nil];     [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillhide:)                                                  name:uikeyboardwillhidenotification object:nil];      [chatservice shared].delegate = self;      self.messagearray = [nsmutablearray arraywitharray:[[chatservice shared] messagsfordialogid:self.dialog.id]];       // set title     if(self.dialog.type == qbchatdialogtypeprivate){         qbuuser *recipient = [chatservice shared].usersasdictionary[@(self.dialog.recipientid)];         self.title = recipient.login == nil ? recipient.email : recipient.login;     }else{         self.title = self.dialog.name;     }      // sync messages history     //     [self syncmessages:no]; }  - (void)viewwilldisappear:(bool)animated{     [super viewwilldisappear:animated];      [chatservice shared].delegate = nil; }  -(bool)hidesbottombarwhenpushed {     return yes; }  - (void)getpreviousmessages{      // load more messages here     //     [self syncmessages:yes]; }  - (void)syncmessages:(bool)loadprevious{      nsarray *messages = [[chatservice shared] messagsfordialogid:self.dialog.id];     nsdate *lastmessagedatesent = nil;     nsdate *firstmessagedatesent = nil;     if(messages.count > 0){         lastmessagedatesent = ((qbchatmessage *)[messages lastobject]).datesent;         firstmessagedatesent = ((qbchatmessage *)[messages firstobject]).datesent;     }       nsmutabledictionary *extendedrequest = [[nsmutabledictionary alloc] init];     if(loadprevious){         if(firstmessagedatesent != nil){             extendedrequest[@"date_sent[lte]"] = @([firstmessagedatesent timeintervalsince1970]-1);         }     }else{         if(lastmessagedatesent != nil){             extendedrequest[@"date_sent[gte]"] = @([lastmessagedatesent timeintervalsince1970]+1);         }     }     extendedrequest[@"sort_desc"] = @"date_sent";      qbresponsepage *page = [qbresponsepage responsepagewithlimit:100 skip:0];     [qbrequest messageswithdialogid:self.dialog.id                     extendedrequest:extendedrequest                             forpage:page                        successblock:^(qbresponse *response, nsarray *messages, qbresponsepage *page) {                            if(messages.count > 0){                                [[chatservice shared] addmessages:messages fordialogid:self.dialog.id];                                nslog(@"messages in request: %lu", messages.count);                            }                             if(loadprevious){                                nsdateformatter *formatter = [[nsdateformatter alloc] init];                                [formatter setdateformat:@"mmm d, h:mm a"];                                nsstring *title = [nsstring stringwithformat:@"last update: %@", [formatter stringfromdate:[nsdate date]]];                                nsdictionary *attrsdictionary = [nsdictionary dictionarywithobject:[uicolor blackcolor]                                                                                            forkey:nsforegroundcolorattributename];                                nsattributedstring *attributedtitle = [[nsattributedstring alloc] initwithstring:title attributes:attrsdictionary];                                self.refreshcontrol.attributedtitle = attributedtitle;                                 [self.refreshcontrol endrefreshing];                                 [self.tableview reloaddata];                            }else{                                [self.tableview reloaddata];                                nsinteger count = [[chatservice shared] messagsfordialogid:self.dialog.id].count;                                if(count > 0){                                    [self.tableview scrolltorowatindexpath:[nsindexpath indexpathforrow:count-1 insection:0]                                                          atscrollposition:uitableviewscrollpositionbottom animated:no];                                }                            }                        } errorblock:^(qbresponse *response) {                         }]; }  #pragma mark #pragma mark actions  - (ibaction)sendmessage:(id)sender{     nsstring *messagetext = self.messagetextfield.text;     if(messagetext.length == 0){         return;     }      qbchatmessage *message = [qbchatmessage message];     message.text = messagetext;     message.markable = yes;     message.dialogid = self.dialog.id;     nsstring *senderlogin = [localstoragecontroller shared].qbuser.login;     nsmutabledictionary *senderlogindictionary = [[nsmutabledictionary alloc]init];     [senderlogindictionary setobject:senderlogin forkey:@"sendername"];     [message setcustomparameters:senderlogindictionary];      [self sendmessagewithtext:messagetext];    //  [[servicesmanager instance].chatservice sendmessage:message todialog:self.dialog save:yes completion:nil];      // clean text field     [self.messagetextfield settext:nil]; }  #pragma mark - sending  - (void) sendmessagewithtext:(nsstring*)text {      // send message     bool sent = [[chatservice shared] sendmessage:text todialog:self.dialog];     if(!sent){         [[twmessagebarmanager sharedinstance] showmessagewithtitle:@"error"                                                        description:@"please check internet connection"                                                               type:twmessagebarmessagetypeinfo];         return;     }      // reload table     [self.tableview reloaddata];     if([[chatservice shared] messagsfordialogid:self.dialog.id].count > 0){         [self.tableview scrolltorowatindexpath:[nsindexpath indexpathforrow:[[[chatservice shared] messagsfordialogid:self.dialog.id] count]-1 insection:0] atscrollposition:uitableviewscrollpositionbottom animated:yes];     } }   #pragma mark #pragma mark uitableviewdelegate & uitableviewdatasource  -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {     return [[[chatservice shared] messagsfordialogid:self.dialog.id] count]; }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {      qbchatmessage *message = [[chatservice shared] messagsfordialogid:self.dialog.id][indexpath.row];      nslog(@"messages in chatservice: %lu", [[chatservice shared] messagsfordialogid:self.dialog.id].count);      chatmessagetableviewcell *cell = [tableview dequeuereusablecellwithidentifier:chatmessagecellidentifier];     [cell configurecellwithmessage:message];  //    static nsstring *cellidentifier = @"messagecell"; //    messagecell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; //    cell.message = message;      cell.userinteractionenabled = no;      return cell; }  -(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath{      qbchatmessage *chatmessage = [[[chatservice shared] messagsfordialogid:self.dialog.id] objectatindex:indexpath.row];      cgfloat cellheight = [chatmessagetableviewcell heightforcellwithmessage:chatmessage];     return cellheight; }  - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath{     [tableview deselectrowatindexpath:indexpath animated:yes]; }   #pragma mark #pragma mark uitextfielddelegate  - (bool)textfieldshouldreturn:(uitextfield *)textfield{     [textfield resignfirstresponder];     return yes; }  #pragma mark - uigesturerecognizer  - (void) messagetextfielddidtap:(uitapgesturerecognizer*) gesture {     [self.messagetextfield becomefirstresponder]; }  #pragma mark #pragma mark keyboard notifications  - (void)keyboardwillshow:(nsnotification *)note {      cgrect keyboardbounds = [note.userinfo[uikeyboardframeenduserinfokey] cgrectvalue];      uiedgeinsets contentinsets = uiedgeinsetsmake(0.0, 0.0, keyboardbounds.size.height, 0.0);      [self.tableview setcontentinset:contentinsets];      [self.tableview setscrollindicatorinsets:contentinsets];      if([[chatservice shared] messagsfordialogid:self.dialog.id].count > 0){         [self.tableview scrolltorowatindexpath:[nsindexpath indexpathforrow:[[[chatservice shared] messagsfordialogid:self.dialog.id] count]-1 insection:0] atscrollposition:uitableviewscrollpositionbottom animated:yes];     }      [uiview animatewithduration:0.3 animations:^{         self.inputbackgroundview.transform = cgaffinetransformmaketranslation(0, -keyboardbounds.size.height);      }];      self.iskeyboradshow = yes; }  - (void)keyboardwillhide:(nsnotification *)note {     self.iskeyboradshow = no;      [self.tableview setcontentinset:uiedgeinsetszero];      [self.tableview setscrollindicatorinsets:uiedgeinsetszero];      if([[chatservice shared] messagsfordialogid:self.dialog.id].count > 0){         [self.tableview scrolltorowatindexpath:[nsindexpath indexpathforrow:[[[chatservice shared] messagsfordialogid:self.dialog.id] count]-1 insection:0] atscrollposition:uitableviewscrollpositionbottom animated:yes];     }      [uiview animatewithduration:0.3 animations:^{         self.inputbackgroundview.transform = cgaffinetransformidentity;      }]; }    #pragma mark #pragma mark chatservicedelegate  - (void)chatdidlogin {     // sync messages history     //     [self syncmessages:no]; }  - (bool)chatdidreceivemessage:(qbchatmessage *)message {      //nsstring *dialogid = message.dialogid;      // reload table      [self syncmessages:yes];     [self.tableview reloaddata];     if([[chatservice shared] messagsfordialogid:self.dialog.id].count > 0){         [self.tableview scrolltorowatindexpath:[nsindexpath indexpathforrow:[[[chatservice shared] messagsfordialogid:self.dialog.id] count]-1 insection:0]                               atscrollposition:uitableviewscrollpositionbottom animated:yes];     }      return yes; }  -(void) chatroomdidreceivemessage:(qbchatmessage *)message fromdialogid:(nsstring *)dialogid {     if (dialogid == self.dialog.id) {      }      [self fetchmessages];     // [self getpreviousmessages];      [self.tableview reloaddata];      if([[chatservice shared] messagsfordialogid:self.dialog.id].count > 0){         [self.tableview scrolltorowatindexpath:[nsindexpath indexpathforrow:[[[chatservice shared] messagsfordialogid:self.dialog.id] count]-1 insection:0]                               atscrollposition:uitableviewscrollpositionbottom animated:yes];      }  }  - (void) hidekeyboard {     [self.messagetextfield resignfirstresponder]; }  - (void) fetchmessages {      nsmutabledictionary *extendedrequest = [nsmutabledictionary new];     nsdate *now = [nsdate date];     extendedrequest[@"date_sent[lte]"]= @([now timeintervalsince1970]);     extendedrequest[@"sort_desc"]= @"date_sent";     // extendedrequest[@"limit"] = @(50);      qbresponsepage *page = [qbresponsepage responsepagewithlimit:100 skip:0];      [qbrequest messageswithdialogid:self.dialog.id extendedrequest:extendedrequest forpage:page successblock:^(qbresponse *response, nsarray *messages, qbresponsepage *page) {         if(messages.count > 0){             [[chatservice shared] addmessages:messages fordialogid:self.dialog.id];             nslog(@"messages in request: %lu", messages.count);             self.messagearray = [nsmutablearray arraywitharray:messages];         }          [self.tableview reloaddata];         nsinteger count = [[chatservice shared] messagsfordialogid:self.dialog.id].count;         if(count > 0){             [self.tableview scrolltorowatindexpath:[nsindexpath indexpathforrow:count-1 insection:0]                                   atscrollposition:uitableviewscrollpositionbottom animated:no];          }       } errorblock:^(qbresponse *response) {       }];  }  #pragma mark - property     @end 

delegates chatroomdidreceivemessage being called correctly, doesn't add new messages ui or whatever. log says "messages insert 1" "dialogs update 1", doesn't update anything.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -