Skip to content

Commit 710bba5

Browse files
Try to fix UIKit inconsistency exception in insertOrMoveContact
The error is: attempt to insert row 0 into section 1, but there are only 0 rows in section 1 after the update
1 parent d3e9b70 commit 710bba5

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

Monal/Classes/ActiveChatsViewController.m

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -335,16 +335,6 @@ -(void) handleDeviceRotation
335335

336336
-(void) refreshDisplay
337337
{
338-
size_t unpinnedConCntBefore = self.unpinnedContacts.count;
339-
size_t pinnedConCntBefore = self.pinnedContacts.count;
340-
NSMutableArray<MLContact*>* newUnpinnedContacts = [[DataLayer sharedInstance] activeContactsWithPinned:NO];
341-
NSMutableArray<MLContact*>* newPinnedContacts = [[DataLayer sharedInstance] activeContactsWithPinned:YES];
342-
if(!newUnpinnedContacts || ! newPinnedContacts)
343-
return;
344-
345-
int unpinnedCntDiff = (int)unpinnedConCntBefore - (int)newUnpinnedContacts.count;
346-
int pinnedCntDiff = (int)pinnedConCntBefore - (int)newPinnedContacts.count;
347-
348338
void (^resizeSections)(UITableView*, size_t, int) = ^void(UITableView* table, size_t section, int diff){
349339
if(diff > 0)
350340
{
@@ -367,6 +357,16 @@ -(void) refreshDisplay
367357
};
368358

369359
dispatch_async(dispatch_get_main_queue(), ^{
360+
size_t unpinnedConCntBefore = self.unpinnedContacts.count;
361+
size_t pinnedConCntBefore = self.pinnedContacts.count;
362+
NSMutableArray<MLContact*>* newUnpinnedContacts = [[DataLayer sharedInstance] activeContactsWithPinned:NO];
363+
NSMutableArray<MLContact*>* newPinnedContacts = [[DataLayer sharedInstance] activeContactsWithPinned:YES];
364+
if(!newUnpinnedContacts || !newPinnedContacts)
365+
return;
366+
367+
int unpinnedCntDiff = (int)unpinnedConCntBefore - (int)newUnpinnedContacts.count;
368+
int pinnedCntDiff = (int)pinnedConCntBefore - (int)newPinnedContacts.count;
369+
370370
//make sure we don't display a chat view for a disabled account
371371
if([MLNotificationManager sharedInstance].currentContact != nil)
372372
{
@@ -508,7 +508,15 @@ -(void) handleNewMessage:(NSNotification*) notification
508508
-(void) insertOrMoveContact:(MLContact*) contact completion:(void (^ _Nullable)(BOOL finished)) completion
509509
{
510510
dispatch_async(dispatch_get_main_queue(), ^{
511-
[self.chatListTable performBatchUpdates:^{
511+
if(self.chatListTable.hasUncommittedUpdates)
512+
{
513+
if(completion) completion(NO);
514+
return;
515+
}
516+
[CATransaction begin];
517+
[UIView performWithoutAnimation:^{
518+
[self.chatListTable beginUpdates];
519+
512520
__block NSIndexPath* indexPath = nil;
513521
for(size_t section = pinnedChats; section < activeChatsViewControllerSectionCnt && !indexPath; section++) {
514522
NSMutableArray* curContactArray = [self getChatArrayForSection:section];
@@ -554,11 +562,16 @@ -(void) insertOrMoveContact:(MLContact*) contact completion:(void (^ _Nullable)(
554562
[self.chatListTable insertRowsAtIndexPaths:@[insertAtPath] withRowAnimation:UITableViewRowAnimationRight];
555563
//make sure to fully refresh to remove the empty dataset (yes this will trigger on first chat pinning, too, but that does no harm)
556564
if(oldCount == 0)
557-
[self refreshDisplay];
565+
dispatch_async(dispatch_get_main_queue(), ^{
566+
[self refreshDisplay];
567+
});
558568
}
559-
} completion:^(BOOL finished) {
560-
if(completion) completion(finished);
569+
570+
[self.chatListTable endUpdates];
561571
}];
572+
[CATransaction commit];
573+
[self.chatListTable reloadEmptyDataSet];
574+
if(completion) completion(YES);
562575
});
563576
}
564577

0 commit comments

Comments
 (0)