@@ -222,8 +222,9 @@ + (void)sortAndCoalesceChanges:(NSMutableArray *)changes
222222
223223  NSEnumerationOptions  options = type == _ASHierarchyChangeTypeDelete ? NSEnumerationReverse : kNilOptions ;
224224  [allIndexes enumerateIndexesWithOptions: options usingBlock: ^(NSUInteger  idx, __unused BOOL  * stop) {
225-     
226225    ASDataControllerAnimationOptions options = [animationOptions[@(idx)] integerValue ];
226+     BOOL  endingCurrentGroup = NO ;
227+     
227228    if  (currentIndexes == nil ) {
228229      //  Starting a new group
229230      currentIndexes = [NSMutableIndexSet  indexSetWithIndex: idx];
@@ -232,21 +233,19 @@ + (void)sortAndCoalesceChanges:(NSMutableArray *)changes
232233      //  Continuing the current group
233234      [currentIndexes addIndex: idx];
234235    } else  {
235-       //  Ending the current group
236+       endingCurrentGroup = YES ;
237+     }
238+     
239+     BOOL  endingLastGroup = (currentIndexes != nil  && allIndexes.lastIndex  == idx);
240+     
241+     if  (endingCurrentGroup || endingLastGroup) {
236242      _ASHierarchySectionChange *change = [[_ASHierarchySectionChange alloc ] initWithChangeType: type indexSet: currentIndexes animationOptions: currentOptions];
237243      [result addObject: change];
238244      currentOptions = 0 ;
239245      currentIndexes = nil ;
240246    }
241247  }];
242248
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-   }
250249  [changes setArray: result];
251250}
252251
@@ -304,9 +303,12 @@ + (void)sortAndCoalesceChanges:(NSMutableArray *)changes ignoringChangesInSectio
304303
305304  ASDataControllerAnimationOptions currentOptions = 0 ;
306305  NSMutableArray  *currentIndexPaths = nil ;
306+   NSIndexPath  *lastIndexPath = allIndexPaths.lastObject ;
307307
308308  for  (NSIndexPath  *indexPath in allIndexPaths) {
309309    ASDataControllerAnimationOptions options = [animationOptions[indexPath] integerValue ];
310+     BOOL  endingCurrentGroup = NO ;
311+     
310312    if  (currentIndexPaths == nil ) {
311313      //  Starting a new group
312314      currentIndexPaths = [NSMutableArray  arrayWithObject: indexPath];
@@ -315,21 +317,19 @@ + (void)sortAndCoalesceChanges:(NSMutableArray *)changes ignoringChangesInSectio
315317      //  Continuing the current group
316318      [currentIndexPaths addObject: indexPath];
317319    } else  {
318-       //  Ending the current group
320+       endingCurrentGroup = YES ;
321+     }
322+     
323+     BOOL  endingLastGroup = (currentIndexPaths != nil  && (NSOrderedSame == [lastIndexPath compare: indexPath]));
324+ 
325+     if  (endingCurrentGroup || endingLastGroup) {
319326      _ASHierarchyItemChange *change = [[_ASHierarchyItemChange alloc ] initWithChangeType: type indexPaths: currentIndexPaths animationOptions: currentOptions presorted: YES ];
320327      [result addObject: change];
321328      currentOptions = 0 ;
322329      currentIndexPaths = nil ;
323330    }
324331  }
325332
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-   }
333333  [changes setArray: result];
334334}
335335
0 commit comments