Carry over first-pass change set improvements

This commit is contained in:
Adlai Holler 2016-06-23 15:52:00 -07:00
parent 77f24231b3
commit 71d9f64535
10 changed files with 224 additions and 60 deletions

View File

@ -546,6 +546,8 @@
CC3B208E1C3F7D0A00798563 /* ASWeakSetTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CC3B208D1C3F7D0A00798563 /* ASWeakSetTests.m */; }; CC3B208E1C3F7D0A00798563 /* ASWeakSetTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CC3B208D1C3F7D0A00798563 /* ASWeakSetTests.m */; };
CC3B20901C3F892D00798563 /* ASBridgedPropertiesTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = CC3B208F1C3F892D00798563 /* ASBridgedPropertiesTests.mm */; }; CC3B20901C3F892D00798563 /* ASBridgedPropertiesTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = CC3B208F1C3F892D00798563 /* ASBridgedPropertiesTests.mm */; };
CC4981B31D1A02BE004E13CC /* ASTableViewThrashTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CC4981B21D1A02BE004E13CC /* ASTableViewThrashTests.m */; }; CC4981B31D1A02BE004E13CC /* ASTableViewThrashTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CC4981B21D1A02BE004E13CC /* ASTableViewThrashTests.m */; };
CC4981BC1D1C7F65004E13CC /* NSIndexSet+ASHelpers.h in Headers */ = {isa = PBXBuildFile; fileRef = CC4981BA1D1C7F65004E13CC /* NSIndexSet+ASHelpers.h */; };
CC4981BD1D1C7F65004E13CC /* NSIndexSet+ASHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = CC4981BB1D1C7F65004E13CC /* NSIndexSet+ASHelpers.m */; };
CC7FD9DE1BB5E962005CCB2B /* ASPhotosFrameworkImageRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = CC7FD9DC1BB5E962005CCB2B /* ASPhotosFrameworkImageRequest.h */; settings = {ATTRIBUTES = (Public, ); }; }; CC7FD9DE1BB5E962005CCB2B /* ASPhotosFrameworkImageRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = CC7FD9DC1BB5E962005CCB2B /* ASPhotosFrameworkImageRequest.h */; settings = {ATTRIBUTES = (Public, ); }; };
CC7FD9DF1BB5E962005CCB2B /* ASPhotosFrameworkImageRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = CC7FD9DD1BB5E962005CCB2B /* ASPhotosFrameworkImageRequest.m */; }; CC7FD9DF1BB5E962005CCB2B /* ASPhotosFrameworkImageRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = CC7FD9DD1BB5E962005CCB2B /* ASPhotosFrameworkImageRequest.m */; };
CC7FD9E11BB5F750005CCB2B /* ASPhotosFrameworkImageRequestTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CC7FD9E01BB5F750005CCB2B /* ASPhotosFrameworkImageRequestTests.m */; }; CC7FD9E11BB5F750005CCB2B /* ASPhotosFrameworkImageRequestTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CC7FD9E01BB5F750005CCB2B /* ASPhotosFrameworkImageRequestTests.m */; };
@ -937,6 +939,8 @@
CC3B208D1C3F7D0A00798563 /* ASWeakSetTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASWeakSetTests.m; sourceTree = "<group>"; }; CC3B208D1C3F7D0A00798563 /* ASWeakSetTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASWeakSetTests.m; sourceTree = "<group>"; };
CC3B208F1C3F892D00798563 /* ASBridgedPropertiesTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASBridgedPropertiesTests.mm; sourceTree = "<group>"; }; CC3B208F1C3F892D00798563 /* ASBridgedPropertiesTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASBridgedPropertiesTests.mm; sourceTree = "<group>"; };
CC4981B21D1A02BE004E13CC /* ASTableViewThrashTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASTableViewThrashTests.m; sourceTree = "<group>"; }; CC4981B21D1A02BE004E13CC /* ASTableViewThrashTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASTableViewThrashTests.m; sourceTree = "<group>"; };
CC4981BA1D1C7F65004E13CC /* NSIndexSet+ASHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSIndexSet+ASHelpers.h"; sourceTree = "<group>"; };
CC4981BB1D1C7F65004E13CC /* NSIndexSet+ASHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSIndexSet+ASHelpers.m"; sourceTree = "<group>"; };
CC7FD9DC1BB5E962005CCB2B /* ASPhotosFrameworkImageRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASPhotosFrameworkImageRequest.h; sourceTree = "<group>"; }; CC7FD9DC1BB5E962005CCB2B /* ASPhotosFrameworkImageRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASPhotosFrameworkImageRequest.h; sourceTree = "<group>"; };
CC7FD9DD1BB5E962005CCB2B /* ASPhotosFrameworkImageRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASPhotosFrameworkImageRequest.m; sourceTree = "<group>"; }; CC7FD9DD1BB5E962005CCB2B /* ASPhotosFrameworkImageRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASPhotosFrameworkImageRequest.m; sourceTree = "<group>"; };
CC7FD9E01BB5F750005CCB2B /* ASPhotosFrameworkImageRequestTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASPhotosFrameworkImageRequestTests.m; sourceTree = "<group>"; }; CC7FD9E01BB5F750005CCB2B /* ASPhotosFrameworkImageRequestTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASPhotosFrameworkImageRequestTests.m; sourceTree = "<group>"; };
@ -1229,6 +1233,8 @@
058D09E1195D050800B7D73C /* Details */ = { 058D09E1195D050800B7D73C /* Details */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
CC4981BA1D1C7F65004E13CC /* NSIndexSet+ASHelpers.h */,
CC4981BB1D1C7F65004E13CC /* NSIndexSet+ASHelpers.m */,
9CFFC6BD1CCAC52B006A6476 /* ASEnvironment.mm */, 9CFFC6BD1CCAC52B006A6476 /* ASEnvironment.mm */,
058D09E2195D050800B7D73C /* _ASDisplayLayer.h */, 058D09E2195D050800B7D73C /* _ASDisplayLayer.h */,
058D09E3195D050800B7D73C /* _ASDisplayLayer.mm */, 058D09E3195D050800B7D73C /* _ASDisplayLayer.mm */,
@ -1634,6 +1640,7 @@
ACF6ED2D1B17843500DA7C62 /* ASRatioLayoutSpec.h in Headers */, ACF6ED2D1B17843500DA7C62 /* ASRatioLayoutSpec.h in Headers */,
AC47D9451B3BB41900AAEE9D /* ASRelativeSize.h in Headers */, AC47D9451B3BB41900AAEE9D /* ASRelativeSize.h in Headers */,
291B63FB1AA53A7A000A71B3 /* ASScrollDirection.h in Headers */, 291B63FB1AA53A7A000A71B3 /* ASScrollDirection.h in Headers */,
CC4981BC1D1C7F65004E13CC /* NSIndexSet+ASHelpers.h in Headers */,
D785F6621A74327E00291744 /* ASScrollNode.h in Headers */, D785F6621A74327E00291744 /* ASScrollNode.h in Headers */,
058D0A7F195D05F900B7D73C /* ASSentinel.h in Headers */, 058D0A7F195D05F900B7D73C /* ASSentinel.h in Headers */,
9C8221951BA237B80037F19A /* ASStackBaselinePositionedLayout.h in Headers */, 9C8221951BA237B80037F19A /* ASStackBaselinePositionedLayout.h in Headers */,
@ -2101,6 +2108,7 @@
ACF6ED4C1B17847A00DA7C62 /* ASInternalHelpers.mm in Sources */, ACF6ED4C1B17847A00DA7C62 /* ASInternalHelpers.mm in Sources */,
68FC85DF1CE29AB700EDD713 /* ASNavigationController.m in Sources */, 68FC85DF1CE29AB700EDD713 /* ASNavigationController.m in Sources */,
ACF6ED251B17843500DA7C62 /* ASLayout.mm in Sources */, ACF6ED251B17843500DA7C62 /* ASLayout.mm in Sources */,
CC4981BD1D1C7F65004E13CC /* NSIndexSet+ASHelpers.m in Sources */,
DB55C2631C6408D6004EDCF5 /* _ASTransitionContext.m in Sources */, DB55C2631C6408D6004EDCF5 /* _ASTransitionContext.m in Sources */,
92074A631CC8BA1900918F75 /* ASImageNode+tvOS.m in Sources */, 92074A631CC8BA1900918F75 /* ASImageNode+tvOS.m in Sources */,
251B8EFA1BBB3D690087C538 /* ASCollectionViewFlowLayoutInspector.m in Sources */, 251B8EFA1BBB3D690087C538 /* ASCollectionViewFlowLayoutInspector.m in Sources */,

View File

@ -145,6 +145,7 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
// Always set, whether ASCollectionView is created directly or via ASCollectionNode. // Always set, whether ASCollectionView is created directly or via ASCollectionNode.
@property (nonatomic, weak) ASTableNode *tableNode; @property (nonatomic, weak) ASTableNode *tableNode;
@property (nonatomic) BOOL test_enableSuperUpdateCallLogging;
@end @end
@implementation ASTableView @implementation ASTableView
@ -974,6 +975,9 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
BOOL preventAnimation = animationOptions == UITableViewRowAnimationNone; BOOL preventAnimation = animationOptions == UITableViewRowAnimationNone;
ASPerformBlockWithoutAnimation(preventAnimation, ^{ ASPerformBlockWithoutAnimation(preventAnimation, ^{
if (self.test_enableSuperUpdateCallLogging) {
NSLog(@"-[super insertRowsAtIndexPaths]: %@", indexPaths);
}
[super insertRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimation)animationOptions]; [super insertRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimation)animationOptions];
[self _scheduleCheckForBatchFetchingForNumberOfChanges:indexPaths.count]; [self _scheduleCheckForBatchFetchingForNumberOfChanges:indexPaths.count];
}); });
@ -994,6 +998,9 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
BOOL preventAnimation = animationOptions == UITableViewRowAnimationNone; BOOL preventAnimation = animationOptions == UITableViewRowAnimationNone;
ASPerformBlockWithoutAnimation(preventAnimation, ^{ ASPerformBlockWithoutAnimation(preventAnimation, ^{
if (self.test_enableSuperUpdateCallLogging) {
NSLog(@"-[super deleteRowsAtIndexPaths]: %@", indexPaths);
}
[super deleteRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimation)animationOptions]; [super deleteRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimation)animationOptions];
[self _scheduleCheckForBatchFetchingForNumberOfChanges:indexPaths.count]; [self _scheduleCheckForBatchFetchingForNumberOfChanges:indexPaths.count];
}); });
@ -1015,6 +1022,9 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
BOOL preventAnimation = animationOptions == UITableViewRowAnimationNone; BOOL preventAnimation = animationOptions == UITableViewRowAnimationNone;
ASPerformBlockWithoutAnimation(preventAnimation, ^{ ASPerformBlockWithoutAnimation(preventAnimation, ^{
if (self.test_enableSuperUpdateCallLogging) {
NSLog(@"-[super insertSections]: %@", indexSet);
}
[super insertSections:indexSet withRowAnimation:(UITableViewRowAnimation)animationOptions]; [super insertSections:indexSet withRowAnimation:(UITableViewRowAnimation)animationOptions];
[self _scheduleCheckForBatchFetchingForNumberOfChanges:indexSet.count]; [self _scheduleCheckForBatchFetchingForNumberOfChanges:indexSet.count];
}); });
@ -1031,6 +1041,9 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
BOOL preventAnimation = animationOptions == UITableViewRowAnimationNone; BOOL preventAnimation = animationOptions == UITableViewRowAnimationNone;
ASPerformBlockWithoutAnimation(preventAnimation, ^{ ASPerformBlockWithoutAnimation(preventAnimation, ^{
if (self.test_enableSuperUpdateCallLogging) {
NSLog(@"-[super deleteSections]: %@", indexSet);
}
[super deleteSections:indexSet withRowAnimation:(UITableViewRowAnimation)animationOptions]; [super deleteSections:indexSet withRowAnimation:(UITableViewRowAnimation)animationOptions];
[self _scheduleCheckForBatchFetchingForNumberOfChanges:indexSet.count]; [self _scheduleCheckForBatchFetchingForNumberOfChanges:indexSet.count];
}); });

View File

@ -34,4 +34,7 @@
*/ */
- (instancetype)_initWithFrame:(CGRect)frame style:(UITableViewStyle)style dataControllerClass:(Class)dataControllerClass ownedByNode:(BOOL)ownedByNode; - (instancetype)_initWithFrame:(CGRect)frame style:(UITableViewStyle)style dataControllerClass:(Class)dataControllerClass ownedByNode:(BOOL)ownedByNode;
/// Set YES and we'll log every time we call [super insertRows…] etc
@property (nonatomic) BOOL test_enableSuperUpdateCallLogging;
@end @end

View File

@ -14,6 +14,7 @@
#import "ASInternalHelpers.h" #import "ASInternalHelpers.h"
#import "_ASHierarchyChangeSet.h" #import "_ASHierarchyChangeSet.h"
#import "ASAssert.h" #import "ASAssert.h"
#import "NSIndexSet+ASHelpers.h"
#import "ASDataController+Subclasses.h" #import "ASDataController+Subclasses.h"
@ -47,10 +48,18 @@
[super beginUpdates]; [super beginUpdates];
for (_ASHierarchyItemChange *change in [_changeSet itemChangesOfType:_ASHierarchyChangeTypeReload]) {
[super deleteRowsAtIndexPaths:change.indexPaths withAnimationOptions:change.animationOptions];
}
for (_ASHierarchyItemChange *change in [_changeSet itemChangesOfType:_ASHierarchyChangeTypeDelete]) { for (_ASHierarchyItemChange *change in [_changeSet itemChangesOfType:_ASHierarchyChangeTypeDelete]) {
[super deleteRowsAtIndexPaths:change.indexPaths withAnimationOptions:change.animationOptions]; [super deleteRowsAtIndexPaths:change.indexPaths withAnimationOptions:change.animationOptions];
} }
for (_ASHierarchySectionChange *change in [_changeSet sectionChangesOfType:_ASHierarchyChangeTypeReload]) {
[super deleteSections:change.indexSet withAnimationOptions:change.animationOptions];
}
for (_ASHierarchySectionChange *change in [_changeSet sectionChangesOfType:_ASHierarchyChangeTypeDelete]) { for (_ASHierarchySectionChange *change in [_changeSet sectionChangesOfType:_ASHierarchyChangeTypeDelete]) {
[super deleteSections:change.indexSet withAnimationOptions:change.animationOptions]; [super deleteSections:change.indexSet withAnimationOptions:change.animationOptions];
} }
@ -58,13 +67,12 @@
// TODO: Shouldn't reloads be processed before deletes, since deletes affect // TODO: Shouldn't reloads be processed before deletes, since deletes affect
// the index space and reloads don't? // the index space and reloads don't?
for (_ASHierarchySectionChange *change in [_changeSet sectionChangesOfType:_ASHierarchyChangeTypeReload]) { for (_ASHierarchySectionChange *change in [_changeSet sectionChangesOfType:_ASHierarchyChangeTypeReload]) {
[super reloadSections:change.indexSet withAnimationOptions:change.animationOptions]; NSIndexSet *newIndexes = [change.indexSet as_indexesByMapping:^(NSUInteger idx) {
return [_changeSet newSectionForOldSection:idx];
}];
[super insertSections:newIndexes withAnimationOptions:change.animationOptions];
} }
for (_ASHierarchyItemChange *change in [_changeSet itemChangesOfType:_ASHierarchyChangeTypeReload]) {
[super reloadRowsAtIndexPaths:change.indexPaths withAnimationOptions:change.animationOptions];
}
for (_ASHierarchySectionChange *change in [_changeSet sectionChangesOfType:_ASHierarchyChangeTypeInsert]) { for (_ASHierarchySectionChange *change in [_changeSet sectionChangesOfType:_ASHierarchyChangeTypeInsert]) {
[super insertSections:change.indexSet withAnimationOptions:change.animationOptions]; [super insertSections:change.indexSet withAnimationOptions:change.animationOptions];
} }

View File

@ -0,0 +1,23 @@
//
// NSIndexSet+ASHelpers.h
// AsyncDisplayKit
//
// Created by Adlai Holler on 6/23/16.
// Copyright © 2016 Facebook. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSIndexSet (ASHelpers)
- (NSIndexSet *)as_indexesByMapping:(NSUInteger (^)(NSUInteger))block;
- (NSIndexSet *)as_intersectionWithIndexes:(NSIndexSet *)indexes;
/// Returns all the item indexes from the given index paths that are in the given section.
+ (NSIndexSet *)as_indexSetFromIndexPaths:(NSArray<NSIndexPath *> *)indexPaths inSection:(NSUInteger)section;
/// If you've got an old index, and you insert items using this index set, this returns the new index.
- (NSUInteger)as_indexByInsertingItemsBelowIndex:(NSUInteger)index;
@end

View File

@ -0,0 +1,62 @@
//
// NSIndexSet+ASHelpers.m
// AsyncDisplayKit
//
// Created by Adlai Holler on 6/23/16.
// Copyright © 2016 Facebook. All rights reserved.
//
@import UIKit;
#import "NSIndexSet+ASHelpers.h"
@implementation NSIndexSet (ASHelpers)
- (NSIndexSet *)as_indexesByMapping:(NSUInteger (^)(NSUInteger))block
{
NSMutableIndexSet *result = [NSMutableIndexSet indexSet];
[self enumerateIndexesUsingBlock:^(NSUInteger idx, __unused BOOL * _Nonnull stop) {
NSUInteger newIndex = block(idx);
if (newIndex != NSNotFound) {
[result addIndex:newIndex];
}
}];
return result;
}
- (NSIndexSet *)as_intersectionWithIndexes:(NSIndexSet *)indexes
{
NSMutableIndexSet *result = [NSMutableIndexSet indexSet];
[self enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) {
[indexes enumerateRangesInRange:range options:kNilOptions usingBlock:^(NSRange range, BOOL * _Nonnull stop) {
[result addIndexesInRange:range];
}];
}];
return result;
}
+ (NSIndexSet *)as_indexSetFromIndexPaths:(NSArray<NSIndexPath *> *)indexPaths inSection:(NSUInteger)section
{
NSMutableIndexSet *result = [NSMutableIndexSet indexSet];
for (NSIndexPath *indexPath in indexPaths) {
if (indexPath.section == section) {
[result addIndex:indexPath.item];
}
}
return result;
}
- (NSUInteger)as_indexByInsertingItemsBelowIndex:(NSUInteger)index
{
__block NSUInteger newIndex = index;
[self enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
if (idx <= newIndex) {
newIndex += 1;
} else {
*stop = YES;
}
}];
return newIndex;
}
@end

View File

@ -13,6 +13,8 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <AsyncDisplayKit/ASInternalHelpers.h> #import <AsyncDisplayKit/ASInternalHelpers.h>
NS_ASSUME_NONNULL_BEGIN
typedef NSUInteger ASDataControllerAnimationOptions; typedef NSUInteger ASDataControllerAnimationOptions;
typedef NS_ENUM(NSInteger, _ASHierarchyChangeType) { typedef NS_ENUM(NSInteger, _ASHierarchyChangeType) {
@ -34,11 +36,11 @@ typedef NS_ENUM(NSInteger, _ASHierarchyChangeType) {
@property (nonatomic, readonly) ASDataControllerAnimationOptions animationOptions; @property (nonatomic, readonly) ASDataControllerAnimationOptions animationOptions;
/// Index paths are sorted descending for changeType .Delete, ascending otherwise /// Index paths are sorted descending for changeType .Delete, ascending otherwise
@property (nonatomic, strong, readonly) NSArray *indexPaths; @property (nonatomic, strong, readonly) NSArray<NSIndexPath *> *indexPaths;
@property (nonatomic, readonly) _ASHierarchyChangeType changeType; @property (nonatomic, readonly) _ASHierarchyChangeType changeType;
+ (NSDictionary *)sectionToIndexSetMapFromChanges:(NSArray *)changes ofType:(_ASHierarchyChangeType)changeType; + (NSDictionary *)sectionToIndexSetMapFromChanges:(NSArray<_ASHierarchyItemChange *> *)changes ofType:(_ASHierarchyChangeType)changeType;
@end @end
@interface _ASHierarchyChangeSet : NSObject @interface _ASHierarchyChangeSet : NSObject
@ -56,7 +58,15 @@ typedef NS_ENUM(NSInteger, _ASHierarchyChangeType) {
@precondition The change set must be completed. @precondition The change set must be completed.
@returns The new section index, or NSNotFound if the given section was deleted. @returns The new section index, or NSNotFound if the given section was deleted.
*/ */
- (NSInteger)newSectionForOldSection:(NSInteger)oldSection; - (NSUInteger)newSectionForOldSection:(NSUInteger)oldSection;
/**
Get the index path after the update for the item at the given index path before the update.
@precondition The change set must be completed.
@returns The new index path, or nil if the given item (or its section) was deleted.
*/
- (nullable NSIndexPath *)newIndexPathForOldIndexPath:(NSIndexPath *)indexPath;
@property (nonatomic, readonly) BOOL completed; @property (nonatomic, readonly) BOOL completed;
@ -77,13 +87,18 @@ typedef NS_ENUM(NSInteger, _ASHierarchyChangeType) {
- Inserted sections, ascending order - Inserted sections, ascending order
- Inserted items, ascending order - Inserted items, ascending order
*/ */
- (NSArray /*<_ASHierarchySectionChange *>*/ *)sectionChangesOfType:(_ASHierarchyChangeType)changeType; - (NSArray <_ASHierarchySectionChange *> *)sectionChangesOfType:(_ASHierarchyChangeType)changeType;
- (NSArray /*<_ASHierarchyItemChange *>*/ *)itemChangesOfType:(_ASHierarchyChangeType)changeType; - (NSArray <_ASHierarchyItemChange *> *)itemChangesOfType:(_ASHierarchyChangeType)changeType;
/// Returns all item indexes affected by changes of the given type in the given section.
- (NSIndexSet *)indexesForItemChangesOfType:(_ASHierarchyChangeType)changeType inSection:(NSUInteger)section;
- (void)deleteSections:(NSIndexSet *)sections animationOptions:(ASDataControllerAnimationOptions)options; - (void)deleteSections:(NSIndexSet *)sections animationOptions:(ASDataControllerAnimationOptions)options;
- (void)insertSections:(NSIndexSet *)sections animationOptions:(ASDataControllerAnimationOptions)options; - (void)insertSections:(NSIndexSet *)sections animationOptions:(ASDataControllerAnimationOptions)options;
- (void)reloadSections:(NSIndexSet *)sections animationOptions:(ASDataControllerAnimationOptions)options; - (void)reloadSections:(NSIndexSet *)sections animationOptions:(ASDataControllerAnimationOptions)options;
- (void)insertItems:(NSArray *)indexPaths animationOptions:(ASDataControllerAnimationOptions)options; - (void)insertItems:(NSArray<NSIndexPath *> *)indexPaths animationOptions:(ASDataControllerAnimationOptions)options;
- (void)deleteItems:(NSArray *)indexPaths animationOptions:(ASDataControllerAnimationOptions)options; - (void)deleteItems:(NSArray<NSIndexPath *> *)indexPaths animationOptions:(ASDataControllerAnimationOptions)options;
- (void)reloadItems:(NSArray *)indexPaths animationOptions:(ASDataControllerAnimationOptions)options; - (void)reloadItems:(NSArray<NSIndexPath *> *)indexPaths animationOptions:(ASDataControllerAnimationOptions)options;
@end @end
NS_ASSUME_NONNULL_END

View File

@ -12,6 +12,7 @@
#import "_ASHierarchyChangeSet.h" #import "_ASHierarchyChangeSet.h"
#import "ASInternalHelpers.h" #import "ASInternalHelpers.h"
#import "NSIndexSet+ASHelpers.h"
@interface _ASHierarchySectionChange () @interface _ASHierarchySectionChange ()
- (instancetype)initWithChangeType:(_ASHierarchyChangeType)changeType indexSet:(NSIndexSet *)indexSet animationOptions:(ASDataControllerAnimationOptions)animationOptions; - (instancetype)initWithChangeType:(_ASHierarchyChangeType)changeType indexSet:(NSIndexSet *)indexSet animationOptions:(ASDataControllerAnimationOptions)animationOptions;
@ -23,7 +24,7 @@
+ (void)sortAndCoalesceChanges:(NSMutableArray *)changes; + (void)sortAndCoalesceChanges:(NSMutableArray *)changes;
/// Returns all the indexes from all the `indexSet`s of the given `_ASHierarchySectionChange` objects. /// Returns all the indexes from all the `indexSet`s of the given `_ASHierarchySectionChange` objects.
+ (NSMutableIndexSet *)allIndexesInChanges:(NSArray *)changes; + (NSMutableIndexSet *)allIndexesInSectionChanges:(NSArray *)changes;
@end @end
@interface _ASHierarchyItemChange () @interface _ASHierarchyItemChange ()
@ -36,14 +37,29 @@
+ (void)sortAndCoalesceChanges:(NSMutableArray *)changes ignoringChangesInSections:(NSIndexSet *)sections; + (void)sortAndCoalesceChanges:(NSMutableArray *)changes ignoringChangesInSections:(NSIndexSet *)sections;
@end @end
@implementation NSIndexSet (ASHierarchyHelpers)
- (NSIndexSet *)intersectionWithIndexes:(NSIndexSet *)indexes
{
NSMutableIndexSet *result = [NSMutableIndexSet indexSet];
[self enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) {
[indexes enumerateRangesInRange:range options:kNilOptions usingBlock:^(NSRange range, BOOL * _Nonnull stop) {
[result addIndexesInRange:range];
}];
}];
return result;
}
@end
@interface _ASHierarchyChangeSet () @interface _ASHierarchyChangeSet ()
@property (nonatomic, strong, readonly) NSMutableArray *insertItemChanges; @property (nonatomic, strong, readonly) NSMutableArray<_ASHierarchyItemChange *> *insertItemChanges;
@property (nonatomic, strong, readonly) NSMutableArray *deleteItemChanges; @property (nonatomic, strong, readonly) NSMutableArray<_ASHierarchyItemChange *> *deleteItemChanges;
@property (nonatomic, strong, readonly) NSMutableArray *reloadItemChanges; @property (nonatomic, strong, readonly) NSMutableArray<_ASHierarchyItemChange *> *reloadItemChanges;
@property (nonatomic, strong, readonly) NSMutableArray *insertSectionChanges; @property (nonatomic, strong, readonly) NSMutableArray<_ASHierarchySectionChange *> *insertSectionChanges;
@property (nonatomic, strong, readonly) NSMutableArray *deleteSectionChanges; @property (nonatomic, strong, readonly) NSMutableArray<_ASHierarchySectionChange *> *deleteSectionChanges;
@property (nonatomic, strong, readonly) NSMutableArray *reloadSectionChanges; @property (nonatomic, strong, readonly) NSMutableArray<_ASHierarchySectionChange *> *reloadSectionChanges;
@end @end
@ -103,24 +119,52 @@
} }
} }
- (NSInteger)newSectionForOldSection:(NSInteger)oldSection - (NSIndexSet *)indexesForItemChangesOfType:(_ASHierarchyChangeType)changeType inSection:(NSUInteger)section
{
[self _ensureCompleted];
NSMutableIndexSet *result = [NSMutableIndexSet indexSet];
for (_ASHierarchyItemChange *change in [self itemChangesOfType:changeType]) {
[result addIndexes:[NSIndexSet as_indexSetFromIndexPaths:change.indexPaths inSection:section]];
}
return result;
}
- (NSUInteger)newSectionForOldSection:(NSUInteger)oldSection
{ {
[self _ensureCompleted]; [self _ensureCompleted];
if ([_deletedSections containsIndex:oldSection]) { if ([_deletedSections containsIndex:oldSection]) {
return NSNotFound; return NSNotFound;
} }
__block NSInteger newIndex = oldSection - [_deletedSections countOfIndexesInRange:NSMakeRange(0, oldSection)]; NSUInteger newIndex = oldSection - [_deletedSections countOfIndexesInRange:NSMakeRange(0, oldSection)];
[_insertedSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) { newIndex = [_insertedSections as_indexByInsertingItemsBelowIndex:newIndex];
if (idx <= newIndex) {
newIndex += 1;
} else {
*stop = YES;
}
}];
return newIndex; return newIndex;
} }
- (nullable NSIndexPath *)newIndexPathForOldIndexPath:(NSIndexPath *)indexPath
{
[self _ensureCompleted];
// If section was deleted, nil
NSUInteger oldSection = indexPath.section;
NSUInteger newSection = [self newSectionForOldSection:oldSection];
if (newSection == NSNotFound) {
return nil;
}
NSUInteger newItem = indexPath.item;
NSIndexSet *deletedItemsInOldSection = [self indexesForItemChangesOfType:_ASHierarchyChangeTypeDelete inSection:oldSection];
newItem -= [deletedItemsInOldSection countOfIndexesInRange:NSMakeRange(0, newItem)];
for (_ASHierarchyItemChange *change in _deleteItemChanges) {
// If item was deleted, nil
if ([change.indexPaths containsObject:indexPath]) {
return nil;
}
}
return [NSIndexPath indexPathForItem:newItem inSection:newSection];
}
- (void)deleteItems:(NSArray *)indexPaths animationOptions:(ASDataControllerAnimationOptions)options - (void)deleteItems:(NSArray *)indexPaths animationOptions:(ASDataControllerAnimationOptions)options
{ {
[self _ensureNotCompleted]; [self _ensureNotCompleted];
@ -184,10 +228,13 @@
[_ASHierarchySectionChange sortAndCoalesceChanges:_insertSectionChanges]; [_ASHierarchySectionChange sortAndCoalesceChanges:_insertSectionChanges];
[_ASHierarchySectionChange sortAndCoalesceChanges:_reloadSectionChanges]; [_ASHierarchySectionChange sortAndCoalesceChanges:_reloadSectionChanges];
_deletedSections = [[_ASHierarchySectionChange allIndexesInChanges:_deleteSectionChanges] copy]; _deletedSections = [[_ASHierarchySectionChange allIndexesInSectionChanges:_deleteSectionChanges] copy];
_insertedSections = [[_ASHierarchySectionChange allIndexesInChanges:_insertSectionChanges] copy]; _insertedSections = [[_ASHierarchySectionChange allIndexesInSectionChanges:_insertSectionChanges] copy];
_reloadedSections = [[_ASHierarchySectionChange allIndexesInChanges:_reloadSectionChanges] copy]; _reloadedSections = [[_ASHierarchySectionChange allIndexesInSectionChanges:_reloadSectionChanges] copy];
NSIndexSet *deletedAndReloaded = [_deletedSections intersectionWithIndexes:_reloadedSections];
NSAssert(deletedAndReloaded.count == 0, @"Request to delete and reload the same section(s): %@", deletedAndReloaded);
// These are invalid old section indexes. // These are invalid old section indexes.
NSMutableIndexSet *deletedOrReloaded = [_deletedSections mutableCopy]; NSMutableIndexSet *deletedOrReloaded = [_deletedSections mutableCopy];
[deletedOrReloaded addIndexes:_reloadedSections]; [deletedOrReloaded addIndexes:_reloadedSections];
@ -223,39 +270,22 @@
// - delete/reload indexPaths that are passed in should all be their current indexPaths // - delete/reload indexPaths that are passed in should all be their current indexPaths
// - insert indexPaths that are passed in should all be their future indexPaths after deletions // - insert indexPaths that are passed in should all be their future indexPaths after deletions
for (NSIndexPath *indexPath in change.indexPaths) { for (NSIndexPath *indexPath in change.indexPaths) {
__block NSUInteger section = indexPath.section; NSUInteger section = [self newSectionForOldSection:indexPath.section];
__block NSUInteger row = indexPath.row; NSUInteger item = indexPath.item;
// Update section number based on section insertions/deletions that are above the current section
section -= [_deletedSections countOfIndexesInRange:NSMakeRange(0, section)];
[_insertedSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
if (idx <= section) {
section += 1;
} else {
*stop = YES;
}
}];
// Update row number based on deletions that are above the current row in the current section // Update row number based on deletions that are above the current row in the current section
NSIndexSet *indicesDeletedInSection = deletedIndexPathsMap[@(indexPath.section)]; NSIndexSet *indicesDeletedInSection = deletedIndexPathsMap[@(indexPath.section)];
row -= [indicesDeletedInSection countOfIndexesInRange:NSMakeRange(0, row)]; item -= [indicesDeletedInSection countOfIndexesInRange:NSMakeRange(0, item)];
// Update row number based on insertions that are above the current row in the future section // Update row number based on insertions that are above the current row in the future section
NSIndexSet *indicesInsertedInSection = insertedIndexPathsMap[@(section)]; NSIndexSet *indicesInsertedInSection = insertedIndexPathsMap[@(section)];
[indicesInsertedInSection enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) { item = [indicesInsertedInSection as_indexByInsertingItemsBelowIndex:item];
if (idx <= row) {
row += 1;
} else {
*stop = YES;
}
}];
//TODO: reuse the old indexPath object if section and row aren't changed //TODO: reuse the old indexPath object if section and row aren't changed
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section]; NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
[newIndexPaths addObject:newIndexPath]; [newIndexPaths addObject:newIndexPath];
} }
// All reload changes are coalesced into deletes and inserts // All reload changes are translated into deletes and inserts
// We delete the items that needs reload together with other deleted items, at their original index // We delete the items that needs reload together with other deleted items, at their original index
_ASHierarchyItemChange *deleteItemChangeFromReloadChange = [[_ASHierarchyItemChange alloc] initWithChangeType:_ASHierarchyChangeTypeDelete indexPaths:change.indexPaths animationOptions:change.animationOptions presorted:NO]; _ASHierarchyItemChange *deleteItemChangeFromReloadChange = [[_ASHierarchyItemChange alloc] initWithChangeType:_ASHierarchyChangeTypeDelete indexPaths:change.indexPaths animationOptions:change.animationOptions presorted:NO];
[_deleteItemChanges addObject:deleteItemChangeFromReloadChange]; [_deleteItemChanges addObject:deleteItemChangeFromReloadChange];
@ -346,7 +376,7 @@
[changes setArray:result]; [changes setArray:result];
} }
+ (NSMutableIndexSet *)allIndexesInChanges:(NSArray *)changes + (NSMutableIndexSet *)allIndexesInSectionChanges:(NSArray<_ASHierarchySectionChange *> *)changes
{ {
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet]; NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
for (_ASHierarchySectionChange *change in changes) { for (_ASHierarchySectionChange *change in changes) {
@ -387,9 +417,9 @@
NSNumber *sectionKey = @(indexPath.section); NSNumber *sectionKey = @(indexPath.section);
NSMutableIndexSet *indexSet = sectionToIndexSetMap[sectionKey]; NSMutableIndexSet *indexSet = sectionToIndexSetMap[sectionKey];
if (indexSet) { if (indexSet) {
[indexSet addIndex:indexPath.row]; [indexSet addIndex:indexPath.item];
} else { } else {
indexSet = [NSMutableIndexSet indexSetWithIndex:indexPath.row]; indexSet = [NSMutableIndexSet indexSetWithIndex:indexPath.item];
sectionToIndexSetMap[sectionKey] = indexSet; sectionToIndexSetMap[sectionKey] = indexSet;
} }
} }

View File

@ -8,6 +8,7 @@
@import XCTest; @import XCTest;
#import <AsyncDisplayKit/AsyncDisplayKit.h> #import <AsyncDisplayKit/AsyncDisplayKit.h>
#import "ASTableViewInternal.h"
// Set to 1 to use UITableView and see if the issue still exists. // Set to 1 to use UITableView and see if the issue still exists.
#define USE_UIKIT_REFERENCE 0 #define USE_UIKIT_REFERENCE 0
@ -477,6 +478,7 @@ static NSInteger ASThrashUpdateCurrentSerializationVersion = 1;
} }
ASThrashDataSource *ds = [[ASThrashDataSource alloc] initWithData:_update.oldData]; ASThrashDataSource *ds = [[ASThrashDataSource alloc] initWithData:_update.oldData];
ds.tableView.test_enableSuperUpdateCallLogging = YES;
[self applyUpdate:_update toDataSource:ds]; [self applyUpdate:_update toDataSource:ds];
[self verifyDataSource:ds]; [self verifyDataSource:ds];
} }

File diff suppressed because one or more lines are too long