diff --git a/AsyncDisplayKit/ASCollectionView.mm b/AsyncDisplayKit/ASCollectionView.mm index edaf045b0c..00e12997c6 100644 --- a/AsyncDisplayKit/ASCollectionView.mm +++ b/AsyncDisplayKit/ASCollectionView.mm @@ -348,11 +348,9 @@ static BOOL _isInterceptedSelector(SEL sel) } } - #pragma mark - #pragma mark ASRangeControllerDelegate. - - (NSArray *)rangeControllerVisibleNodeIndexPaths:(ASRangeController *)rangeController { ASDisplayNodeAssertMainThread(); @@ -372,36 +370,28 @@ static BOOL _isInterceptedSelector(SEL sel) - (void)rangeController:(ASRangeController *)rangeController didInsertNodesAtIndexPaths:(NSArray *)indexPaths { ASDisplayNodeAssertMainThread(); [UIView performWithoutAnimation:^{ - [self performBatchUpdates:^{ - [super insertItemsAtIndexPaths:indexPaths]; - } completion:nil]; + [super insertItemsAtIndexPaths:indexPaths]; }]; } - (void)rangeController:(ASRangeController *)rangeController didDeleteNodesAtIndexPaths:(NSArray *)indexPaths { ASDisplayNodeAssertMainThread(); [UIView performWithoutAnimation:^{ - [self performBatchUpdates:^{ - [super deleteItemsAtIndexPaths:indexPaths]; - } completion:nil]; + [super deleteItemsAtIndexPaths:indexPaths]; }]; } - (void)rangeController:(ASRangeController *)rangeController didInsertSectionsAtIndexSet:(NSIndexSet *)indexSet { ASDisplayNodeAssertMainThread(); [UIView performWithoutAnimation:^{ - [self performBatchUpdates:^{ - [super insertSections:indexSet]; - } completion:nil]; + [super insertSections:indexSet]; }]; } - (void)rangeController:(ASRangeController *)rangeController didDeleteSectionsAtIndexSet:(NSIndexSet *)indexSet { ASDisplayNodeAssertMainThread(); [UIView performWithoutAnimation:^{ - [self performBatchUpdates:^{ - [super deleteSections:indexSet]; - } completion:nil]; + [super deleteSections:indexSet]; }]; } diff --git a/AsyncDisplayKit/ASTableView.mm b/AsyncDisplayKit/ASTableView.mm index 719c7c7ee1..e2289902cb 100644 --- a/AsyncDisplayKit/ASTableView.mm +++ b/AsyncDisplayKit/ASTableView.mm @@ -361,9 +361,7 @@ static BOOL _isInterceptedSelector(SEL sel) ASDisplayNodeAssertMainThread(); [UIView performWithoutAnimation:^{ - [super beginUpdates]; [super insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone]; - [super endUpdates]; }]; } @@ -371,9 +369,7 @@ static BOOL _isInterceptedSelector(SEL sel) ASDisplayNodeAssertMainThread(); [UIView performWithoutAnimation:^{ - [super beginUpdates]; [super deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone]; - [super endUpdates]; }]; } @@ -381,9 +377,7 @@ static BOOL _isInterceptedSelector(SEL sel) ASDisplayNodeAssertMainThread(); [UIView performWithoutAnimation:^{ - [super beginUpdates]; [super insertSections:indexSet withRowAnimation:UITableViewRowAnimationNone]; - [super endUpdates]; }]; } @@ -391,9 +385,7 @@ static BOOL _isInterceptedSelector(SEL sel) ASDisplayNodeAssertMainThread(); [UIView performWithoutAnimation:^{ - [super beginUpdates]; [super deleteSections:indexSet withRowAnimation:UITableViewRowAnimationNone]; - [super endUpdates]; }]; } diff --git a/AsyncDisplayKit/Details/ASRangeController.h b/AsyncDisplayKit/Details/ASRangeController.h index 4befde4bb9..d32de1ddfd 100644 --- a/AsyncDisplayKit/Details/ASRangeController.h +++ b/AsyncDisplayKit/Details/ASRangeController.h @@ -95,4 +95,26 @@ */ - (void)rangeController:(ASRangeController *)rangeController didDeleteSectionsAtIndexSet:(NSIndexSet *)indexSet; +@optional + +/** + * Called before nodes insertion. + */ +- (void)rangeController:(ASRangeController *)rangeController willInsertNodesAtIndexPaths:(NSArray *)indexPaths; + +/** + * Called before nodes deletion. + */ +- (void)rangeController:(ASRangeController *)rangeController willDeleteNodesAtIndexPaths:(NSArray *)indexPaths; + +/** + * Called before section insertion. + */ +- (void)rangeController:(ASRangeController *)rangeController willInsertSectionsAtIndexSet:(NSIndexSet *)indexSet; + +/** + * Called before section deletion. + */ +- (void)rangeController:(ASRangeController *)rangeController willDeleteSectionsAtIndexSet:(NSIndexSet *)indexSet; + @end diff --git a/AsyncDisplayKit/Details/ASRangeController.mm b/AsyncDisplayKit/Details/ASRangeController.mm index 51a3e678df..4dacf56582 100644 --- a/AsyncDisplayKit/Details/ASRangeController.mm +++ b/AsyncDisplayKit/Details/ASRangeController.mm @@ -213,18 +213,12 @@ #pragma mark - ASDataControllerDelegete -/** - * Dispatch to main thread for updating ranges. - * We are considering to move it to background queue if we could call recursive display in background thread. - */ -- (void)updateOnMainThreadWithBlock:(dispatch_block_t)block { - if ([NSThread isMainThread]) { - block(); - } else { - dispatch_async(dispatch_get_main_queue(), ^{ - block(); - }); - } +- (void)dataController:(ASDataController *)dataController willInsertNodes:(NSArray *)nodes atIndexPaths:(NSArray *)indexPaths { + ASDisplayNodePerformBlockOnMainThread(^{ + if ([_delegate respondsToSelector:@selector(rangeController:willInsertNodesAtIndexPaths:)]) { + [_delegate rangeController:self willInsertNodesAtIndexPaths:indexPaths]; + } + }); } - (void)dataController:(ASDataController *)dataController didInsertNodes:(NSArray *)nodes atIndexPaths:(NSArray *)indexPaths { @@ -235,19 +229,35 @@ [nodeSizes addObject:[NSValue valueWithCGSize:node.calculatedSize]]; }]; - [self updateOnMainThreadWithBlock:^{ + ASDisplayNodePerformBlockOnMainThread(^{ [_layoutController insertNodesAtIndexPaths:indexPaths withSizes:nodeSizes]; [_delegate rangeController:self didInsertNodesAtIndexPaths:indexPaths]; _workingRangeIsValid = NO; - }]; + }); +} + +- (void)dataController:(ASDataController *)dataController willDeleteNodesAtIndexPaths:(NSArray *)indexPaths { + ASDisplayNodePerformBlockOnMainThread(^{ + if ([_delegate respondsToSelector:@selector(rangeController:willDeleteNodesAtIndexPaths:)]) { + [_delegate rangeController:self didDeleteNodesAtIndexPaths:indexPaths]; + } + }); } - (void)dataController:(ASDataController *)dataController didDeleteNodesAtIndexPaths:(NSArray *)indexPaths { - [self updateOnMainThreadWithBlock:^{ + ASDisplayNodePerformBlockOnMainThread(^{ [_layoutController deleteNodesAtIndexPaths:indexPaths]; [_delegate rangeController:self didDeleteNodesAtIndexPaths:indexPaths]; _workingRangeIsValid = NO; - }]; + }); +} + +- (void)dataController:(ASDataController *)dataController willInsertSections:(NSArray *)sections atIndexSet:(NSIndexSet *)indexSet { + ASDisplayNodePerformBlockOnMainThread(^{ + if ([_delegate respondsToSelector:@selector(rangeController:willInsertSectionsAtIndexSet:)]) { + [_delegate rangeController:self willInsertSectionsAtIndexSet:indexSet]; + } + }); } - (void)dataController:(ASDataController *)dataController didInsertSections:(NSArray *)sections atIndexSet:(NSIndexSet *)indexSet { @@ -263,19 +273,27 @@ [sectionNodeSizes addObject:nodeSizes]; }]; - [self updateOnMainThreadWithBlock:^{ + ASDisplayNodePerformBlockOnMainThread(^{ [_layoutController insertSections:sectionNodeSizes atIndexSet:indexSet]; [_delegate rangeController:self didInsertSectionsAtIndexSet:indexSet]; _workingRangeIsValid = NO; - }]; + }); +} + +- (void)dataController:(ASDataController *)dataController willDeleteSectionsAtIndexSet:(NSIndexSet *)indexSet { + ASDisplayNodePerformBlockOnMainThread(^{ + if ([_delegate respondsToSelector:@selector(rangeController:willDeleteSectionsAtIndexSet:)]) { + [_delegate rangeController:self didDeleteSectionsAtIndexSet:indexSet]; + } + }); } - (void)dataController:(ASDataController *)dataController didDeleteSectionsAtIndexSet:(NSIndexSet *)indexSet { - [self updateOnMainThreadWithBlock:^{ + ASDisplayNodePerformBlockOnMainThread(^{ [_layoutController deleteSectionsAtIndexSet:indexSet]; [_delegate rangeController:self didDeleteSectionsAtIndexSet:indexSet]; _workingRangeIsValid = NO; - }]; + }); } @end