Expand ASExperimentalRangeUpdateOnChangesetUpdate to ASTableView (#1979)

A previous commit
(https://github.com/TextureGroup/Texture/commit/8f7444e0ece61d6ab12ecceb590be26d8d7cc99d)
aimed to fix a preloading bug for ASCollectionView. This commit expands this
fix to ASTableView as the bug occurs there too.

Previous commit message for context:

This experiment makes sure a ASCollectionView's `rangeController` updates when
a changeset WITH updates is applied. Currently it is possible for nodes
inserted into the preload range to not get preloaded when performing a batch
update.

For example, suppose a collection node has:
- Tuning parameters with a preload range of 1 screenful for the given range
  mode.
- Nodes A and B where A is visible and B is off screen.
Currently if node B is deleted and a new node C is inserted in its place, node
C will not get preloaded until the collection node is scrolled. This is because
the preloading mechanism relies on a `setNeedsUpdate` call on the range
controller as part of the `-collectionView:willDisplayCell:forItemAtIndexPath:`
delegate method when the batch update is submitted. However, in the example
outlined above, this sometimes doesn't happen automtically, causing the range
update to be delayed until the next the view scrolls.
This commit is contained in:
rqueue
2021-03-25 16:20:29 -07:00
committed by GitHub
parent 05b81a5f3e
commit dca5a223b7
2 changed files with 129 additions and 1 deletions
+3
View File
@@ -1679,6 +1679,9 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
LOG(@"--- UITableView endUpdates");
ASPerformBlockWithoutAnimation(!changeSet.animated, ^{
[super endUpdates];
if (numberOfUpdates > 0 && ASActivateExperimentalFeature(ASExperimentalRangeUpdateOnChangesetUpdate)) {
[self->_rangeController setNeedsUpdate];
}
[self->_rangeController updateIfNeeded];
[self _scheduleCheckForBatchFetchingForNumberOfChanges:numberOfUpdates];
});
+126 -1
View File
@@ -118,6 +118,7 @@
@interface ASTestTextCellNode : ASTextCellNode
/** Calculated by counting how many times -layoutSpecThatFits: is called on the main thread. */
@property (nonatomic) int numberOfLayoutsOnMainThread;
@property (nonatomic) NSUInteger didEnterPreloadStateCount;
@end
@implementation ASTestTextCellNode
@@ -130,6 +131,12 @@
return [super layoutSpecThatFits:constrainedSize];
}
- (void)didEnterPreloadState
{
[super didEnterPreloadState];
_didEnterPreloadStateCount++;
}
@end
@interface ASTableViewFilledDataSource : NSObject <ASTableDataSource, ASTableDelegate>
@@ -232,6 +239,36 @@
@end
@interface ATableViewTestController: UIViewController
@property (nonatomic) ASTableNode *tableNode;
@property (nonatomic) ASTableViewFilledDataSource *dataSource;
@end
@implementation ATableViewTestController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
ASTableNode *tableNode = [[ASTableNode alloc] initWithStyle:UITableViewStylePlain];
tableNode.frame = CGRectMake(0, 0, 100, 500);
ASTableViewFilledDataSource *dataSource = [ASTableViewFilledDataSource new];
tableNode.delegate = dataSource;
tableNode.dataSource = dataSource;
self.tableNode = tableNode;
self.dataSource = dataSource;
[self.view addSubview:self.tableNode.view];
}
return self;
}
@end
@interface ASTableViewTests : ASTestCase
@property (nonatomic, retain) ASTableView *testTableView;
@end
@@ -242,7 +279,8 @@
{
[super setUp];
ASConfiguration *config = [ASConfiguration new];
config.experimentalFeatures = ASExperimentalOptimizeDataControllerPipeline;
config.experimentalFeatures = ASExperimentalOptimizeDataControllerPipeline
| ASExperimentalRangeUpdateOnChangesetUpdate;
[ASConfigurationManager test_resetWithConfiguration:config];
}
@@ -761,6 +799,93 @@
[node performBatchAnimated:NO updates:nil completion:nil];
}
- (void)testItemsInsertedIntoThePreloadRangeGetPreloaded
{
// Start table node setup
ATableViewTestController *testController = [[ATableViewTestController alloc] initWithNibName:nil bundle:nil];
ASTableNode *tableNode = testController.tableNode;
ASTableViewFilledDataSource *dataSource = testController.dataSource;
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = testController;
[window makeKeyAndVisible];
ASRangeTuningParameters minimumPreloadParams = { .leadingBufferScreenfuls = 1, .trailingBufferScreenfuls = 1 };
[tableNode setTuningParameters:minimumPreloadParams forRangeMode:ASLayoutRangeModeMinimum rangeType:ASLayoutRangeTypePreload];
[tableNode updateCurrentRangeWithMode:ASLayoutRangeModeMinimum];
[tableNode reloadData];
[tableNode waitUntilAllUpdatesAreProcessed];
[testController.tableNode.view layoutIfNeeded];
// End table node setup
NSIndexPath *lastVisibleIndex = [[tableNode indexPathsForVisibleRows] sortedArrayUsingSelector:@selector(compare:)].lastObject;
NSInteger itemCount = dataSource.rowsPerSection;
BOOL isLastItemInSection = lastVisibleIndex.row == itemCount - 1;
NSInteger nextItemSection = isLastItemInSection ? lastVisibleIndex.section + 1 : lastVisibleIndex.section;
NSInteger nextItemRow = isLastItemInSection ? 0 : lastVisibleIndex.row + 1;
XCTAssertTrue(dataSource.numberOfSections > nextItemSection, @"There is no items after the last visible item. Update the section/row counts so that there is one for this test to work properly.");
XCTAssertTrue(dataSource.rowsPerSection > nextItemRow, @"There is no items after the last visible item. Update the section/row counts so that there is one for this test to work properly.");
NSIndexPath *nextItemIndexPath = [NSIndexPath indexPathForRow:nextItemRow inSection:nextItemSection];
ASTestTextCellNode *nodeBeforeUpdate = (ASTestTextCellNode *)[tableNode nodeForRowAtIndexPath:nextItemIndexPath];
XCTestExpectation *noChangeDone = [self expectationWithDescription:@"Batch update with no changes done and completion block has been called. Tuning params set to 1 screenful."];
__block ASTestTextCellNode *nodeAfterUpdate;
[tableNode performBatchUpdates:^{
} completion:^(BOOL finished) {
nodeAfterUpdate = (ASTestTextCellNode *)[tableNode nodeForRowAtIndexPath:nextItemIndexPath];
[noChangeDone fulfill];
}];
[self waitForExpectations:@[ noChangeDone ] timeout:1];
XCTAssertTrue(nodeBeforeUpdate == nodeAfterUpdate, @"Node should not have changed since no updates were made.");
XCTAssertTrue(nodeAfterUpdate.didEnterPreloadStateCount == 1, @"Node should have been preloaded.");
XCTestExpectation *changeDone = [self expectationWithDescription:@"Batch update with changes done and completion block has been called. Tuning params set to 1 screenful."];
[tableNode performBatchUpdates:^{
NSArray *indexPaths = @[ nextItemIndexPath ];
[tableNode deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[tableNode insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
} completion:^(BOOL finished) {
nodeAfterUpdate = (ASTestTextCellNode *)[tableNode nodeForRowAtIndexPath:nextItemIndexPath];
[changeDone fulfill];
}];
[self waitForExpectations:@[ changeDone ] timeout:1];
XCTAssertTrue(nodeBeforeUpdate != nodeAfterUpdate, @"Node should have changed after updating.");
XCTAssertTrue(nodeAfterUpdate.didEnterPreloadStateCount == 1, @"New node should have been preloaded.");
minimumPreloadParams = { .leadingBufferScreenfuls = 0, .trailingBufferScreenfuls = 0 };
[tableNode setTuningParameters:minimumPreloadParams forRangeMode:ASLayoutRangeModeMinimum rangeType:ASLayoutRangeTypePreload];
[tableNode updateCurrentRangeWithMode:ASLayoutRangeModeMinimum];
XCTestExpectation *changeDoneZeroSreenfuls = [self expectationWithDescription:@"Batch update with changes done and completion block has been called. Tuning params set to 0 screenful."];
nodeBeforeUpdate = nodeAfterUpdate;
__block ASTestTextCellNode *nodeAfterUpdateZeroSreenfuls;
[tableNode performBatchUpdates:^{
NSArray *indexPaths = @[ nextItemIndexPath ];
[tableNode deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[tableNode insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
} completion:^(BOOL finished) {
nodeAfterUpdateZeroSreenfuls = (ASTestTextCellNode *)[tableNode nodeForRowAtIndexPath:nextItemIndexPath];
[changeDoneZeroSreenfuls fulfill];
}];
[self waitForExpectations:@[ changeDoneZeroSreenfuls ] timeout:1];
XCTAssertTrue(nodeBeforeUpdate != nodeAfterUpdateZeroSreenfuls, @"Node should have changed after updating.");
XCTAssertTrue(nodeAfterUpdateZeroSreenfuls.didEnterPreloadStateCount == 0, @"New node should NOT have been preloaded.");
}
// https://github.com/facebook/AsyncDisplayKit/issues/2252#issuecomment-263689979
- (void)testIssue2252
{