@@ -219,11 +219,13 @@ + (void)sortAndCoalesceChanges:(NSMutableArray *)changes
219219
220220  __block ASDataControllerAnimationOptions currentOptions = 0 ;
221221  __block NSMutableIndexSet  *currentIndexes = nil ;
222+   NSUInteger  lastIndex = allIndexes.lastIndex ;
222223
223224  NSEnumerationOptions  options = type == _ASHierarchyChangeTypeDelete ? NSEnumerationReverse : kNilOptions ;
224225  [allIndexes enumerateIndexesWithOptions: options usingBlock: ^(NSUInteger  idx, __unused BOOL  * stop) {
225-     
226226    ASDataControllerAnimationOptions options = [animationOptions[@(idx)] integerValue ];
227+     BOOL  endingCurrentGroup = NO ;
228+     
227229    if  (currentIndexes == nil ) {
228230      //  Starting a new group
229231      currentIndexes = [NSMutableIndexSet  indexSetWithIndex: idx];
@@ -232,21 +234,19 @@ + (void)sortAndCoalesceChanges:(NSMutableArray *)changes
232234      //  Continuing the current group
233235      [currentIndexes addIndex: idx];
234236    } else  {
235-       //  Ending the current group
237+       endingCurrentGroup = YES ;
238+     }
239+     
240+     BOOL  endingLastGroup = (currentIndexes != nil  && lastIndex == idx);
241+     
242+     if  (endingCurrentGroup || endingLastGroup) {
236243      _ASHierarchySectionChange *change = [[_ASHierarchySectionChange alloc ] initWithChangeType: type indexSet: currentIndexes animationOptions: currentOptions];
237244      [result addObject: change];
238245      currentOptions = 0 ;
239246      currentIndexes = nil ;
240247    }
241248  }];
242249
243-   if  (currentIndexes != nil ) {
244-     //  Ending the last group
245-     _ASHierarchySectionChange *change = [[_ASHierarchySectionChange alloc ] initWithChangeType: type indexSet: currentIndexes animationOptions: currentOptions];
246-     [result addObject: change];
247-     currentOptions = 0 ;
248-     currentIndexes = nil ;
249-   }
250250  [changes setArray: result];
251251}
252252
@@ -304,9 +304,12 @@ + (void)sortAndCoalesceChanges:(NSMutableArray *)changes ignoringChangesInSectio
304304
305305  ASDataControllerAnimationOptions currentOptions = 0 ;
306306  NSMutableArray  *currentIndexPaths = nil ;
307+   NSIndexPath  *lastIndexPath = allIndexPaths.lastObject ;
307308
308309  for  (NSIndexPath  *indexPath in allIndexPaths) {
309310    ASDataControllerAnimationOptions options = [animationOptions[indexPath] integerValue ];
311+     BOOL  endingCurrentGroup = NO ;
312+     
310313    if  (currentIndexPaths == nil ) {
311314      //  Starting a new group
312315      currentIndexPaths = [NSMutableArray  arrayWithObject: indexPath];
@@ -315,21 +318,19 @@ + (void)sortAndCoalesceChanges:(NSMutableArray *)changes ignoringChangesInSectio
315318      //  Continuing the current group
316319      [currentIndexPaths addObject: indexPath];
317320    } else  {
318-       //  Ending the current group
321+       endingCurrentGroup = YES ;
322+     }
323+     
324+     BOOL  endingLastGroup = (currentIndexPaths != nil  && (NSOrderedSame == [lastIndexPath compare: indexPath]));
325+ 
326+     if  (endingCurrentGroup || endingLastGroup) {
319327      _ASHierarchyItemChange *change = [[_ASHierarchyItemChange alloc ] initWithChangeType: type indexPaths: currentIndexPaths animationOptions: currentOptions presorted: YES ];
320328      [result addObject: change];
321329      currentOptions = 0 ;
322330      currentIndexPaths = nil ;
323331    }
324332  }
325333
326-   if  (currentIndexPaths != nil ) {
327-     //  Ending the last group
328-     _ASHierarchyItemChange *change = [[_ASHierarchyItemChange alloc ] initWithChangeType: type indexPaths: currentIndexPaths animationOptions: currentOptions presorted: YES ];
329-     [result addObject: change];
330-     currentOptions = 0 ;
331-     currentIndexPaths = nil ;
332-   }
333334  [changes setArray: result];
334335}
335336
0 commit comments