Skip to content

Commit c33d6ef

Browse files
leviLevi McCallum
authored andcommitted
WIP synchronous reload data on collection view
1 parent a88ad0a commit c33d6ef

22 files changed

Lines changed: 1216 additions & 1 deletion

AsyncDisplayKit/ASCollectionView.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@
131131
*/
132132
- (void)reloadData;
133133

134+
- (void)reloadDataAndWait;
135+
134136
/**
135137
* Registers the given kind of supplementary node for use in creating node-backed supplementary views.
136138
*

AsyncDisplayKit/ASCollectionView.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ - (void)reloadData
280280
[self reloadDataWithCompletion:nil];
281281
}
282282

283+
- (void)reloadDataAndWait
284+
{
285+
[_dataController reloadDataAndWait];
286+
[super reloadData];
287+
}
288+
283289
- (void)setDataSource:(id<UICollectionViewDataSource>)dataSource
284290
{
285291
// UIKit can internally generate a call to this method upon changing the asyncDataSource; only assert for non-nil.

AsyncDisplayKit/Details/ASDataController.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ typedef NSUInteger ASDataControllerAnimationOptions;
168168

169169
- (void)reloadDataWithAnimationOptions:(ASDataControllerAnimationOptions)animationOptions completion:(void (^)())completion;
170170

171+
- (void)reloadDataAndWait;
172+
171173
/** @name Data Querying */
172174

173175
- (NSUInteger)numberOfSections;

AsyncDisplayKit/Details/ASDataController.mm

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,51 @@ - (void)reloadDataWithAnimationOptions:(ASDataControllerAnimationOptions)animati
404404
}];
405405
}
406406

407+
- (void)reloadDataAndWait
408+
{
409+
[self performEditCommandWithBlock:^{
410+
ASDisplayNodeAssertMainThread();
411+
[_editingTransactionQueue waitUntilAllOperationsAreFinished];
412+
413+
[self accessDataSourceSynchronously:YES withBlock:^{
414+
NSUInteger sectionCount = [_dataSource numberOfSectionsInDataController:self];
415+
NSMutableArray *updatedNodes = [NSMutableArray array];
416+
NSMutableArray *updatedIndexPaths = [NSMutableArray array];
417+
[self _populateFromEntireDataSourceWithMutableNodes:updatedNodes mutableIndexPaths:updatedIndexPaths];
418+
419+
// Measure nodes whose views are loaded before we leave the main thread
420+
[self layoutLoadedNodes:updatedNodes ofKind:ASDataControllerRowNodeKind atIndexPaths:updatedIndexPaths];
421+
422+
// Allow subclasses to perform setup before going into the edit transaction
423+
[self prepareForReloadData];
424+
425+
LOG(@"Edit Transaction - reloadData");
426+
427+
ASDataControllerAnimationOptions animationOptions = UITableViewRowAnimationNone;
428+
429+
// Remove everything that existed before the reload, now that we're ready to insert replacements
430+
NSArray *indexPaths = ASIndexPathsForMultidimensionalArray(_editingNodes[ASDataControllerRowNodeKind]);
431+
[self _deleteNodesAtIndexPaths:indexPaths withAnimationOptions:animationOptions];
432+
433+
NSMutableArray *editingNodes = _editingNodes[ASDataControllerRowNodeKind];
434+
NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, editingNodes.count)];
435+
[self _deleteSectionsAtIndexSet:indexSet withAnimationOptions:animationOptions];
436+
437+
[self willReloadData];
438+
439+
// Insert each section
440+
NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
441+
for (int i = 0; i < sectionCount; i++) {
442+
[sections addObject:[[NSMutableArray alloc] init]];
443+
}
444+
445+
[self _insertSections:sections atIndexSet:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, sectionCount)] withAnimationOptions:animationOptions];
446+
447+
[self _batchLayoutNodes:updatedNodes atIndexPaths:updatedIndexPaths withAnimationOptions:animationOptions];
448+
}];
449+
}];
450+
}
451+
407452
#pragma mark - Data Source Access (Calling _dataSource)
408453

409454
/**
@@ -413,7 +458,12 @@ - (void)reloadDataWithAnimationOptions:(ASDataControllerAnimationOptions)animati
413458
*/
414459
- (void)accessDataSourceWithBlock:(dispatch_block_t)block
415460
{
416-
if (_asyncDataFetchingEnabled) {
461+
[self accessDataSourceSynchronously:NO withBlock:block];
462+
}
463+
464+
- (void)accessDataSourceSynchronously:(BOOL)synchronously withBlock:(dispatch_block_t)block
465+
{
466+
if (!synchronously && _asyncDataFetchingEnabled) {
417467
[_dataSource dataControllerLockDataSource];
418468
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
419469
block();
17.1 KB
Loading
17.9 KB
Loading
22.8 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://github.com/CocoaPods/Specs.git'
2+
platform :ios, '8.0'
3+
pod 'AsyncDisplayKit', :path => '../..'

0 commit comments

Comments
 (0)