iPhone Developer iPhone Developer - 4 years ago 161
iOS Question

JSQMessagesController textview stuck

My TextView is freezing after sending 2-3 messages. My iOS version is 10.2.1 and testing device is iPhone 6Plus, JSQMessagesViewController version is 7.3.4 and the code is in Obj-C. I have implemented the following method :

-(void)didPressSendButton:(UIButton *)button withMessageText:(NSString *)text senderId:(NSString *)senderId senderDisplayName:(NSString *)senderDisplayName date:(NSDate *)date
{
[self addMessagewithId:senderId name:senderDisplayName date:date andText:text inArray:_messages inGroup:nil];

NSDateFormatter *dateF = [[NSDateFormatter alloc] init];
dateF.dateFormat= @"yyyy-MM-dd hh:mm:ss zzzz";
NSString *dateStr = [dateF stringFromDate:date];

NSDictionary *mdata = @{@"text": text, @"senderId":senderId, @"displayName": senderDisplayName, @"date":dateStr};

// Push data to Firebase Database
[[[_rootRef child:@"messages"] childByAutoId] setValue:mdata];

[self finishSendingMessageAnimated:NO];

_isTyping =false;
[self sendIsTyping];
}

-(void)addMessagewithId:(NSString*) senderId name:(NSString*) name date:(NSDate*)date andText:(NSString*) text inArray:(NSMutableArray*)array inGroup:(dispatch_group_t)group1
{

JSQMessage* message = [[JSQMessage alloc] initWithSenderId:senderId senderDisplayName:name date:date text:text];

[array addObject:message];

if (group1) {
dispatch_group_leave(group1);
}

}


But after calling

[self finishSendingMessageAnimated:NO];


the textview starts freezing and the cursor stays where the text ends. This freezing time increases as user sends more messages. Please help.

Answer Source

The issue in my code was that after calling the method

[[[_rootRef child:@"messages"] childByAutoId] setValue:mdata];

it was again reloading all the collectionview as expected from FIRDataEventTypeValue . So I put an array like this :

 [[[_rootRef child:@"messages"] queryOrderedByKey] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
for (FIRDataSnapshot *child in snapshot.children) {

                    if ([_arrSnap containsObject:child.key]) {
                        return;
                    }
                    else
                    {
                        [_arrSnap addObject:child.key];

                        //do rest of your coding here
                    }
}
}

so the reloading stopped for my collectionview and all the extra execution of code stopped.

Also thanks to @danielleonard for his reply. It led me to this solution.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download