Augustyniak / RATreeView

Library providing easy-to-use interface for displaying tree structures on iOS and tvOS.
MIT License
2.49k stars 466 forks source link

Crash at removing a few items inside beginUpdates/endUpdates #235

Open apozdeyev opened 6 years ago

apozdeyev commented 6 years ago

Hello, We use RATreeView in our project together with NSFetchedResultsController, but we get a problem when CoreDate context is saved with a few deleted objects.

I dived into RATreeView code and found a place with possible issue: file - RATreeView+Private.m, method - removeItemAtIndex:inParent:withAnimation:

- (void)removeItemAtIndex:(NSInteger)index inParent:(id)parent withAnimation:(RATreeViewRowAnimation)animation
{
  id child = [self.treeNodeCollectionController childInParent:parent atIndex:index];
  NSInteger idx = [self.treeNodeCollectionController lastVisibleDescendantIndexForItem:child];
  if (idx == NSNotFound) {
    return;
  }

  __weak __typeof(self) weakSelf = self;
  [self.batchChanges insertItemWithBlock:^{
    [weakSelf.treeNodeCollectionController removeItemsAtIndexes:[NSIndexSet indexSetWithIndex:index] inParent:parent updates:^(NSIndexSet *removedIndexes) {
      UITableViewRowAnimation tableViewRowAnimation = [RATreeView tableViewRowAnimationForTreeViewRowAnimation:animation];
      [weakSelf.tableView deleteRowsAtIndexPaths:IndexesToIndexPaths(removedIndexes) withRowAnimation:tableViewRowAnimation];
    }];
  } atIndex:idx];
}

Here we add RABatchChangeEntity with type RABatchChangeTypeItemRowInsertion to batchChanges. Later at invoking endUpdates RABatchChangeEntities are sorted by comparing type and changed object index in order to apply them in correct order. Seems it is needed to replace "self.batchChanges insertItemWithBlock" to "self.batchChanges deleteItemWithBlock". But I am not sure that it produce another issues. If anyone faced with such problem, I will appreciate any help.

Also I made a simple demo with this crash. It is modified Example app from RATreeView, rows are removed at tapping Edit button on navbar which produce the crash.