Fixed crash on multiple -[ASTableView reloadData]. Implemented test.

This commit is contained in:
Victor Mayorov
2015-06-19 12:37:38 +03:00
parent 0c7b1051d6
commit 503b16fd62
2 changed files with 44 additions and 21 deletions

View File

@@ -21,7 +21,6 @@
// keys should be ASLayoutRangeTypes and values NSSets containing NSIndexPaths
NSMutableDictionary *_rangeTypeIndexPaths;
NSDictionary *_rangeTypeHandlers;
BOOL _queuedRangeUpdate;
ASScrollDirection _scrollDirection;
}
@@ -68,25 +67,11 @@
- (void)visibleNodeIndexPathsDidChangeWithScrollDirection:(ASScrollDirection)scrollDirection
{
_scrollDirection = scrollDirection;
if (_queuedRangeUpdate) {
return;
}
// coalesce these events -- handling them multiple times per runloop is noisy and expensive
_queuedRangeUpdate = YES;
[self performSelector:@selector(updateVisibleNodeIndexPaths)
withObject:nil
afterDelay:0
inModes:@[ NSRunLoopCommonModes ]];
[self updateVisibleNodeIndexPaths];
}
- (void)updateVisibleNodeIndexPaths
{
if (!_queuedRangeUpdate) {
return;
}
NSArray *visibleNodePaths = [_delegate rangeControllerVisibleNodeIndexPaths:self];
NSSet *visibleNodePathsSet = [NSSet setWithArray:visibleNodePaths];
CGSize viewportSize = [_delegate rangeControllerViewportSize:self];
@@ -140,7 +125,6 @@
}
_rangeIsValid = YES;
_queuedRangeUpdate = NO;
}
- (BOOL)shouldSkipVisibleNodesForRangeType:(ASLayoutRangeType)rangeType
@@ -196,8 +180,8 @@
ASDisplayNodePerformBlockOnMainThread(^{
[_layoutController insertNodesAtIndexPaths:indexPaths withSizes:nodeSizes];
[_delegate rangeController:self didInsertNodesAtIndexPaths:indexPaths withAnimationOption:animationOption];
_rangeIsValid = NO;
[_delegate rangeController:self didInsertNodesAtIndexPaths:indexPaths withAnimationOption:animationOption];
});
}
@@ -212,8 +196,8 @@
- (void)dataController:(ASDataController *)dataController didDeleteNodesAtIndexPaths:(NSArray *)indexPaths withAnimationOption:(ASDataControllerAnimationOptions)animationOption {
ASDisplayNodePerformBlockOnMainThread(^{
[_layoutController deleteNodesAtIndexPaths:indexPaths];
[_delegate rangeController:self didDeleteNodesAtIndexPaths:indexPaths withAnimationOption:animationOption];
_rangeIsValid = NO;
[_delegate rangeController:self didDeleteNodesAtIndexPaths:indexPaths withAnimationOption:animationOption];
});
}
@@ -240,8 +224,8 @@
ASDisplayNodePerformBlockOnMainThread(^{
[_layoutController insertSections:sectionNodeSizes atIndexSet:indexSet];
[_delegate rangeController:self didInsertSectionsAtIndexSet:indexSet withAnimationOption:animationOption];
_rangeIsValid = NO;
[_delegate rangeController:self didInsertSectionsAtIndexSet:indexSet withAnimationOption:animationOption];
});
}
@@ -256,8 +240,8 @@
- (void)dataController:(ASDataController *)dataController didDeleteSectionsAtIndexSet:(NSIndexSet *)indexSet withAnimationOption:(ASDataControllerAnimationOptions)animationOption {
ASDisplayNodePerformBlockOnMainThread(^{
[_layoutController deleteSectionsAtIndexSet:indexSet];
[_delegate rangeController:self didDeleteSectionsAtIndexSet:indexSet withAnimationOption:animationOption];
_rangeIsValid = NO;
[_delegate rangeController:self didDeleteSectionsAtIndexSet:indexSet withAnimationOption:animationOption];
});
}

View File

@@ -52,6 +52,27 @@
@end
@interface ASTableViewFilledDataSource : NSObject <ASTableViewDataSource, ASTableViewDelegate>
@end
@implementation ASTableViewFilledDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
- (ASCellNode *)tableView:(ASTableView *)tableView nodeForRowAtIndexPath:(NSIndexPath *)indexPath
{
ASTextCellNode *textCellNode = [ASTextCellNode new];
textCellNode.text = indexPath.description;
return textCellNode;
}
@end
@interface ASTableViewTests : XCTestCase
@end
@@ -83,4 +104,22 @@
XCTAssertTrue(tableViewDidDealloc, @"unexpected table view lifetime:%@", tableView);
}
- (void)testReloadData
{
ASTableView *tableView = [[ASTableView alloc] initWithFrame:CGRectMake(0, 0, 1000, 1000)
style:UITableViewStylePlain
asyncDataFetching:YES];
ASTableViewFilledDataSource *dataSource = [ASTableViewFilledDataSource new];
tableView.asyncDelegate = dataSource;
tableView.asyncDataSource = dataSource;
for (int i = 0; i < 100; ++i)
{
[tableView reloadData];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
}
}
@end