Skip to content

Commit 38f1efd

Browse files
nguyenhuyAdlai Holler
authored andcommitted
[ASDataController] Simplify data controller (facebookarchive#2923)
* Start removing ASChangeSetDataController * Continue removing ASChangeSetDataController * Remove unnecessary change * ASDataController is no longer an abstract class, remove its assertion * Get back beginUpdates and endUpdatesAnimated:completion in ASCollectionNode
1 parent 70c48ba commit 38f1efd

16 files changed

Lines changed: 257 additions & 345 deletions

AsyncDisplayKit.xcodeproj/project.pbxproj

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,6 @@
321321
A2763D7A1CBDD57D00A9ADBD /* ASPINRemoteImageDownloader.h in Headers */ = {isa = PBXBuildFile; fileRef = A2763D771CBDD57D00A9ADBD /* ASPINRemoteImageDownloader.h */; };
322322
A37320101C571B740011FC94 /* ASTextNode+Beta.h in Headers */ = {isa = PBXBuildFile; fileRef = A373200E1C571B050011FC94 /* ASTextNode+Beta.h */; settings = {ATTRIBUTES = (Public, ); }; };
323323
AC026B581BD3F61800BBC17E /* ASAbsoluteLayoutSpecSnapshotTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AC026B571BD3F61800BBC17E /* ASAbsoluteLayoutSpecSnapshotTests.m */; };
324-
AC026B6A1BD57D6F00BBC17E /* ASChangeSetDataController.h in Headers */ = {isa = PBXBuildFile; fileRef = AC026B671BD57D6F00BBC17E /* ASChangeSetDataController.h */; settings = {ATTRIBUTES = (Public, ); }; };
325-
AC026B6B1BD57D6F00BBC17E /* ASChangeSetDataController.mm in Sources */ = {isa = PBXBuildFile; fileRef = AC026B681BD57D6F00BBC17E /* ASChangeSetDataController.mm */; };
326-
AC026B6C1BD57D6F00BBC17E /* ASChangeSetDataController.mm in Sources */ = {isa = PBXBuildFile; fileRef = AC026B681BD57D6F00BBC17E /* ASChangeSetDataController.mm */; };
327324
AC026B701BD57DBF00BBC17E /* _ASHierarchyChangeSet.h in Headers */ = {isa = PBXBuildFile; fileRef = AC026B6D1BD57DBF00BBC17E /* _ASHierarchyChangeSet.h */; };
328325
AC026B711BD57DBF00BBC17E /* _ASHierarchyChangeSet.mm in Sources */ = {isa = PBXBuildFile; fileRef = AC026B6E1BD57DBF00BBC17E /* _ASHierarchyChangeSet.mm */; };
329326
AC026B721BD57DBF00BBC17E /* _ASHierarchyChangeSet.mm in Sources */ = {isa = PBXBuildFile; fileRef = AC026B6E1BD57DBF00BBC17E /* _ASHierarchyChangeSet.mm */; };
@@ -578,7 +575,6 @@
578575
F7CE6C4E1D2CDB3E00BE4C15 /* ASThread.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 058D0A12195D050800B7D73C /* ASThread.h */; };
579576
F7CE6C4F1D2CDB3E00BE4C15 /* CoreGraphics+ASConvenience.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 205F0E1F1B376416007741D0 /* CoreGraphics+ASConvenience.h */; };
580577
F7CE6C501D2CDB3E00BE4C15 /* ASDataController.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 464052191A3F83C40061C0BA /* ASDataController.h */; };
581-
F7CE6C511D2CDB3E00BE4C15 /* ASChangeSetDataController.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = AC026B671BD57D6F00BBC17E /* ASChangeSetDataController.h */; };
582578
F7CE6C521D2CDB3E00BE4C15 /* ASIndexedNodeContext.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = E5711A2A1C840C81009619D4 /* ASIndexedNodeContext.h */; };
583579
F7CE6C531D2CDB3E00BE4C15 /* NSMutableAttributedString+TextKitAdditions.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 058D09F5195D050800B7D73C /* NSMutableAttributedString+TextKitAdditions.h */; };
584580
F7CE6C541D2CDB3E00BE4C15 /* _ASAsyncTransaction.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 058D09F8195D050800B7D73C /* _ASAsyncTransaction.h */; };
@@ -757,7 +753,6 @@
757753
F7CE6C4E1D2CDB3E00BE4C15 /* ASThread.h in CopyFiles */,
758754
F7CE6C4F1D2CDB3E00BE4C15 /* CoreGraphics+ASConvenience.h in CopyFiles */,
759755
F7CE6C501D2CDB3E00BE4C15 /* ASDataController.h in CopyFiles */,
760-
F7CE6C511D2CDB3E00BE4C15 /* ASChangeSetDataController.h in CopyFiles */,
761756
F7CE6C521D2CDB3E00BE4C15 /* ASIndexedNodeContext.h in CopyFiles */,
762757
F7CE6C531D2CDB3E00BE4C15 /* NSMutableAttributedString+TextKitAdditions.h in CopyFiles */,
763758
F7CE6C541D2CDB3E00BE4C15 /* _ASAsyncTransaction.h in CopyFiles */,
@@ -1088,8 +1083,6 @@
10881083
A32FEDD31C501B6A004F642A /* ASTextKitFontSizeAdjuster.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ASTextKitFontSizeAdjuster.h; path = TextKit/ASTextKitFontSizeAdjuster.h; sourceTree = "<group>"; };
10891084
A373200E1C571B050011FC94 /* ASTextNode+Beta.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ASTextNode+Beta.h"; sourceTree = "<group>"; };
10901085
AC026B571BD3F61800BBC17E /* ASAbsoluteLayoutSpecSnapshotTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASAbsoluteLayoutSpecSnapshotTests.m; sourceTree = "<group>"; };
1091-
AC026B671BD57D6F00BBC17E /* ASChangeSetDataController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASChangeSetDataController.h; sourceTree = "<group>"; };
1092-
AC026B681BD57D6F00BBC17E /* ASChangeSetDataController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASChangeSetDataController.mm; sourceTree = "<group>"; };
10931086
AC026B6D1BD57DBF00BBC17E /* _ASHierarchyChangeSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _ASHierarchyChangeSet.h; sourceTree = "<group>"; };
10941087
AC026B6E1BD57DBF00BBC17E /* _ASHierarchyChangeSet.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = _ASHierarchyChangeSet.mm; sourceTree = "<group>"; };
10951088
AC21EC0F1B3D0BF600C8B19A /* ASStackLayoutDefines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ASStackLayoutDefines.h; path = AsyncDisplayKit/Layout/ASStackLayoutDefines.h; sourceTree = "<group>"; };
@@ -1695,8 +1688,6 @@
16951688
251B8EF31BBB3D690087C538 /* ASCollectionDataController.mm */,
16961689
464052191A3F83C40061C0BA /* ASDataController.h */,
16971690
4640521A1A3F83C40061C0BA /* ASDataController.mm */,
1698-
AC026B671BD57D6F00BBC17E /* ASChangeSetDataController.h */,
1699-
AC026B681BD57D6F00BBC17E /* ASChangeSetDataController.mm */,
17001691
E5711A2A1C840C81009619D4 /* ASIndexedNodeContext.h */,
17011692
E5711A2D1C840C96009619D4 /* ASIndexedNodeContext.mm */,
17021693
AC6145401D8AFAE8003D62A2 /* ASSection.h */,
@@ -1834,7 +1825,6 @@
18341825
69E0E8A71D356C9400627613 /* ASEqualityHelpers.h in Headers */,
18351826
698C8B621CAB49FC0052DC3F /* ASLayoutElementExtensibility.h in Headers */,
18361827
698548641CA9E025008A345F /* ASEnvironment.h in Headers */,
1837-
AC026B6A1BD57D6F00BBC17E /* ASChangeSetDataController.h in Headers */,
18381828
69F10C871C84C35D0026140C /* ASRangeControllerUpdateRangeProtocol+Beta.h in Headers */,
18391829
B350623C1B010EFD0018CF92 /* _ASAsyncTransaction.h in Headers */,
18401830
9C70F20D1CDBE9CB007D6C76 /* ASDefaultPlayButton.h in Headers */,
@@ -2312,7 +2302,6 @@
23122302
81EE38501C8E94F000456208 /* ASRunLoopQueue.mm in Sources */,
23132303
9C70F2041CDA4EFA007D6C76 /* ASTraitCollection.m in Sources */,
23142304
ACF6ED321B17843500DA7C62 /* ASAbsoluteLayoutSpec.mm in Sources */,
2315-
AC026B6B1BD57D6F00BBC17E /* ASChangeSetDataController.mm in Sources */,
23162305
690C35611E055C5D00069B91 /* ASDimensionInternal.mm in Sources */,
23172306
68355B311CB5799E001D4E68 /* ASImageNode+AnimatedImage.mm in Sources */,
23182307
68C215591DE10D330019C4BC /* ASCollectionViewLayoutInspector.m in Sources */,
@@ -2503,7 +2492,6 @@
25032492
83A7D95B1D44547700BF333E /* ASWeakMap.m in Sources */,
25042493
DE84918E1C8FFF9F003D89E9 /* ASRunLoopQueue.mm in Sources */,
25052494
68FC85E51CE29B7E00EDD713 /* ASTabBarController.m in Sources */,
2506-
AC026B6C1BD57D6F00BBC17E /* ASChangeSetDataController.mm in Sources */,
25072495
34EFC7741B701D0A00AD841F /* ASAbsoluteLayoutSpec.mm in Sources */,
25082496
690C35621E055C5D00069B91 /* ASDimensionInternal.mm in Sources */,
25092497
68C2155A1DE10D330019C4BC /* ASCollectionViewLayoutInspector.m in Sources */,

AsyncDisplayKit/ASCollectionNode.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ - (void)reloadDataImmediately
529529

530530
- (void)beginUpdates
531531
{
532-
[self.dataController beginUpdates];
532+
[self.view beginUpdates];
533533
}
534534

535535
- (void)endUpdatesAnimated:(BOOL)animated
@@ -539,7 +539,7 @@ - (void)endUpdatesAnimated:(BOOL)animated
539539

540540
- (void)endUpdatesAnimated:(BOOL)animated completion:(void (^)(BOOL))completion
541541
{
542-
[self.dataController endUpdatesAnimated:animated completion:completion];
542+
[self.view endUpdatesAnimated:animated completion:completion];
543543
}
544544

545545
- (void)insertSections:(NSIndexSet *)sections

AsyncDisplayKit/ASCollectionView.mm

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#import "ASCollectionViewLayoutFacilitatorProtocol.h"
2727
#import "ASSectionContext.h"
2828
#import "ASCollectionView+Undeprecated.h"
29+
#import "_ASHierarchyChangeSet.h"
2930

3031
/**
3132
* A macro to get self.collectionNode and assign it to a local variable, or return
@@ -196,7 +197,17 @@ @interface ASCollectionView () <ASRangeControllerDataSource, ASRangeControllerDe
196197
* (0 sections) we always check at least once after each update (initial reload is the first update.)
197198
*/
198199
BOOL _hasEverCheckedForBatchFetchingDueToUpdate;
199-
200+
201+
/**
202+
* The change set that we're currently building, if any.
203+
*/
204+
_ASHierarchyChangeSet *_changeSet;
205+
206+
/**
207+
* Counter used to keep track of nested batch updates.
208+
*/
209+
NSInteger _batchUpdateCount;
210+
200211
struct {
201212
unsigned int scrollViewDidScroll:1;
202213
unsigned int scrollViewWillBeginDragging:1;
@@ -352,6 +363,8 @@ - (instancetype)_initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionV
352363
- (void)dealloc
353364
{
354365
ASDisplayNodeAssertMainThread();
366+
ASDisplayNodeCAssert(_batchUpdateCount == 0, @"ASCollectionView deallocated in the middle of a batch update.");
367+
355368
// Sometimes the UIKit classes can call back to their delegate even during deallocation, due to animation completion blocks etc.
356369
_isDeallocating = YES;
357370
[self setAsyncDelegate:nil];
@@ -402,6 +415,12 @@ - (void)relayoutItems
402415
- (void)waitUntilAllUpdatesAreCommitted
403416
{
404417
ASDisplayNodeAssertMainThread();
418+
if (_batchUpdateCount > 0) {
419+
// This assertion will be enabled soon.
420+
// ASDisplayNodeFailAssert(@"Should not call %@ during batch update", NSStringFromSelector(_cmd));
421+
return;
422+
}
423+
405424
[_dataController waitUntilAllUpdatesAreCommitted];
406425
}
407426

@@ -761,15 +780,43 @@ - (ASDataController *)dataController
761780
return _dataController;
762781
}
763782

764-
- (void)performBatchAnimated:(BOOL)animated updates:(void (^)())updates completion:(void (^)(BOOL))completion
783+
- (void)beginUpdates
765784
{
766785
ASDisplayNodeAssertMainThread();
786+
// _changeSet must be available during batch update
787+
ASDisplayNodeAssertTrue((_batchUpdateCount > 0) == (_changeSet != nil));
767788

768-
[_dataController beginUpdates];
789+
if (_batchUpdateCount == 0) {
790+
_changeSet = [[_ASHierarchyChangeSet alloc] initWithOldData:[_dataController itemCountsFromDataSource]];
791+
}
792+
_batchUpdateCount++;
793+
}
794+
795+
- (void)endUpdatesAnimated:(BOOL)animated completion:(nullable void (^)(BOOL))completion
796+
{
797+
ASDisplayNodeAssertMainThread();
798+
ASDisplayNodeAssertNotNil(_changeSet, @"_changeSet must be available when batch update ends");
799+
800+
_batchUpdateCount--;
801+
// Prevent calling endUpdatesAnimated:completion: in an unbalanced way
802+
NSAssert(_batchUpdateCount >= 0, @"endUpdatesAnimated:completion: called without having a balanced beginUpdates call");
803+
804+
[_changeSet addCompletionHandler:completion];
805+
806+
if (_batchUpdateCount == 0) {
807+
[_dataController updateWithChangeSet:_changeSet animated:animated];
808+
_changeSet = nil;
809+
}
810+
}
811+
812+
- (void)performBatchAnimated:(BOOL)animated updates:(void (^)())updates completion:(void (^)(BOOL))completion
813+
{
814+
ASDisplayNodeAssertMainThread();
815+
[self beginUpdates];
769816
if (updates) {
770817
updates();
771818
}
772-
[_dataController endUpdatesAnimated:animated completion:completion];
819+
[self endUpdatesAnimated:animated completion:completion];
773820
}
774821

775822
- (void)performBatchUpdates:(void (^)())updates completion:(void (^)(BOOL))completion
@@ -789,27 +836,35 @@ - (void)insertSections:(NSIndexSet *)sections
789836
{
790837
ASDisplayNodeAssertMainThread();
791838
if (sections.count == 0) { return; }
792-
[_dataController insertSections:sections withAnimationOptions:kASCollectionViewAnimationNone];
839+
[self performBatchUpdates:^{
840+
[_changeSet insertSections:sections animationOptions:kASCollectionViewAnimationNone];
841+
} completion:nil];
793842
}
794843

795844
- (void)deleteSections:(NSIndexSet *)sections
796845
{
797846
ASDisplayNodeAssertMainThread();
798847
if (sections.count == 0) { return; }
799-
[_dataController deleteSections:sections withAnimationOptions:kASCollectionViewAnimationNone];
848+
[self performBatchUpdates:^{
849+
[_changeSet deleteSections:sections animationOptions:kASCollectionViewAnimationNone];
850+
} completion:nil];
800851
}
801852

802853
- (void)reloadSections:(NSIndexSet *)sections
803854
{
804855
ASDisplayNodeAssertMainThread();
805856
if (sections.count == 0) { return; }
806-
[_dataController reloadSections:sections withAnimationOptions:kASCollectionViewAnimationNone];
857+
[self performBatchUpdates:^{
858+
[_changeSet reloadSections:sections animationOptions:kASCollectionViewAnimationNone];
859+
} completion:nil];
807860
}
808861

809862
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection
810863
{
811864
ASDisplayNodeAssertMainThread();
812-
[_dataController moveSection:section toSection:newSection withAnimationOptions:kASCollectionViewAnimationNone];
865+
[self performBatchUpdates:^{
866+
[_changeSet moveSection:section toSection:newSection animationOptions:kASCollectionViewAnimationNone];
867+
} completion:nil];
813868
}
814869

815870
- (id<ASSectionContext>)contextForSection:(NSInteger)section
@@ -822,27 +877,35 @@ - (void)insertItemsAtIndexPaths:(NSArray *)indexPaths
822877
{
823878
ASDisplayNodeAssertMainThread();
824879
if (indexPaths.count == 0) { return; }
825-
[_dataController insertRowsAtIndexPaths:indexPaths withAnimationOptions:kASCollectionViewAnimationNone];
880+
[self performBatchUpdates:^{
881+
[_changeSet insertItems:indexPaths animationOptions:kASCollectionViewAnimationNone];
882+
} completion:nil];
826883
}
827884

828885
- (void)deleteItemsAtIndexPaths:(NSArray *)indexPaths
829886
{
830887
ASDisplayNodeAssertMainThread();
831888
if (indexPaths.count == 0) { return; }
832-
[_dataController deleteRowsAtIndexPaths:indexPaths withAnimationOptions:kASCollectionViewAnimationNone];
889+
[self performBatchUpdates:^{
890+
[_changeSet deleteItems:indexPaths animationOptions:kASCollectionViewAnimationNone];
891+
} completion:nil];
833892
}
834893

835894
- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths
836895
{
837896
ASDisplayNodeAssertMainThread();
838897
if (indexPaths.count == 0) { return; }
839-
[_dataController reloadRowsAtIndexPaths:indexPaths withAnimationOptions:kASCollectionViewAnimationNone];
898+
[self performBatchUpdates:^{
899+
[_changeSet reloadItems:indexPaths animationOptions:kASCollectionViewAnimationNone];
900+
} completion:nil];
840901
}
841902

842903
- (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
843904
{
844905
ASDisplayNodeAssertMainThread();
845-
[_dataController moveRowAtIndexPath:indexPath toIndexPath:newIndexPath withAnimationOptions:kASCollectionViewAnimationNone];
906+
[self performBatchUpdates:^{
907+
[_changeSet moveItemAtIndexPath:indexPath toIndexPath:newIndexPath animationOptions:kASCollectionViewAnimationNone];
908+
} completion:nil];
846909
}
847910

848911
#pragma mark -

0 commit comments

Comments
 (0)