Ensure that new section supplementary nodes are inserted into backing store

This commit is contained in:
Levi McCallum 2015-12-06 20:07:29 -08:00
parent 8f5b498381
commit 64da9e7ece

View File

@ -28,12 +28,20 @@
NSMutableDictionary *_pendingIndexPaths; NSMutableDictionary *_pendingIndexPaths;
} }
- (void)prepareForReloadData - (instancetype)initWithAsyncDataFetching:(BOOL)asyncDataFetchingEnabled
{ {
self = [super initWithAsyncDataFetching:asyncDataFetchingEnabled];
if (self != nil) {
_pendingNodes = [NSMutableDictionary dictionary]; _pendingNodes = [NSMutableDictionary dictionary];
_pendingIndexPaths = [NSMutableDictionary dictionary]; _pendingIndexPaths = [NSMutableDictionary dictionary];
}
return self;
}
[[self supplementaryKinds] enumerateObjectsUsingBlock:^(NSString *kind, NSUInteger idx, BOOL *stop) { - (void)prepareForReloadData
{
NSArray *kinds = [self supplementaryKinds];
for (NSString *kind in kinds) {
LOG(@"Populating elements of kind: %@", kind); LOG(@"Populating elements of kind: %@", kind);
NSMutableArray *indexPaths = [NSMutableArray array]; NSMutableArray *indexPaths = [NSMutableArray array];
NSMutableArray *nodes = [NSMutableArray array]; NSMutableArray *nodes = [NSMutableArray array];
@ -43,7 +51,7 @@
// Measure loaded nodes before leaving the main thread // Measure loaded nodes before leaving the main thread
[self layoutLoadedNodes:nodes ofKind:kind atIndexPaths:indexPaths]; [self layoutLoadedNodes:nodes ofKind:kind atIndexPaths:indexPaths];
}]; }
} }
- (void)willReloadData - (void)willReloadData
@ -75,7 +83,8 @@
- (void)prepareForInsertSections:(NSIndexSet *)sections - (void)prepareForInsertSections:(NSIndexSet *)sections
{ {
[[self supplementaryKinds] enumerateObjectsUsingBlock:^(NSString *kind, NSUInteger idx, BOOL *stop) { NSArray *kinds = [self supplementaryKinds];
for (NSString *kind in kinds) {
LOG(@"Populating elements of kind: %@, for sections: %@", kind, sections); LOG(@"Populating elements of kind: %@, for sections: %@", kind, sections);
NSMutableArray *nodes = [NSMutableArray array]; NSMutableArray *nodes = [NSMutableArray array];
NSMutableArray *indexPaths = [NSMutableArray array]; NSMutableArray *indexPaths = [NSMutableArray array];
@ -85,7 +94,7 @@
// Measure loaded nodes before leaving the main thread // Measure loaded nodes before leaving the main thread
[self layoutLoadedNodes:nodes ofKind:kind atIndexPaths:indexPaths]; [self layoutLoadedNodes:nodes ofKind:kind atIndexPaths:indexPaths];
}]; }
} }
- (void)willInsertSections:(NSIndexSet *)sections - (void)willInsertSections:(NSIndexSet *)sections
@ -97,25 +106,29 @@
} }
[self insertSections:sectionArray ofKind:kind atIndexSet:sections completion:nil]; [self insertSections:sectionArray ofKind:kind atIndexSet:sections completion:nil];
[self batchLayoutNodes:nodes ofKind:kind atIndexPaths:_pendingIndexPaths[kind] completion:nil]; [self batchLayoutNodes:nodes ofKind:kind atIndexPaths:_pendingIndexPaths[kind] completion:^(NSArray *nodes, NSArray *indexPaths) {
_pendingNodes[kind] = nil; [self insertNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil];
_pendingIndexPaths[kind] = nil; }];
[_pendingNodes removeObjectForKey:kind];
[_pendingIndexPaths removeObjectForKey:kind];
}]; }];
} }
- (void)willDeleteSections:(NSIndexSet *)sections - (void)willDeleteSections:(NSIndexSet *)sections
{ {
[[self supplementaryKinds] enumerateObjectsUsingBlock:^(NSString *kind, NSUInteger idx, BOOL *stop) { NSArray *kinds = [self supplementaryKinds];
for (NSString *kind in kinds) {
NSArray *indexPaths = ASIndexPathsForMultidimensionalArrayAtIndexSet([self editingNodesOfKind:kind], sections); NSArray *indexPaths = ASIndexPathsForMultidimensionalArrayAtIndexSet([self editingNodesOfKind:kind], sections);
[self deleteNodesOfKind:kind atIndexPaths:indexPaths completion:nil]; [self deleteNodesOfKind:kind atIndexPaths:indexPaths completion:nil];
[self deleteSectionsOfKind:kind atIndexSet:sections completion:nil]; [self deleteSectionsOfKind:kind atIndexSet:sections completion:nil];
}]; }
} }
- (void)prepareForReloadSections:(NSIndexSet *)sections - (void)prepareForReloadSections:(NSIndexSet *)sections
{ {
[[self supplementaryKinds] enumerateObjectsUsingBlock:^(NSString *kind, NSUInteger idx, BOOL *stop) { NSArray *kinds = [self supplementaryKinds];
for (NSString *kind in kinds) {
NSMutableArray *nodes = [NSMutableArray array]; NSMutableArray *nodes = [NSMutableArray array];
NSMutableArray *indexPaths = [NSMutableArray array]; NSMutableArray *indexPaths = [NSMutableArray array];
[self _populateSupplementaryNodesOfKind:kind withSections:sections mutableNodes:nodes mutableIndexPaths:indexPaths]; [self _populateSupplementaryNodesOfKind:kind withSections:sections mutableNodes:nodes mutableIndexPaths:indexPaths];
@ -124,7 +137,7 @@
// Measure loaded nodes before leaving the main thread // Measure loaded nodes before leaving the main thread
[self layoutLoadedNodes:nodes ofKind:kind atIndexPaths:indexPaths]; [self layoutLoadedNodes:nodes ofKind:kind atIndexPaths:indexPaths];
}]; }
} }
- (void)willReloadSections:(NSIndexSet *)sections - (void)willReloadSections:(NSIndexSet *)sections
@ -134,14 +147,15 @@
[self deleteNodesOfKind:kind atIndexPaths:indexPaths completion:nil]; [self deleteNodesOfKind:kind atIndexPaths:indexPaths completion:nil];
// reinsert the elements // reinsert the elements
[self batchLayoutNodes:nodes ofKind:kind atIndexPaths:_pendingIndexPaths[kind] completion:nil]; [self batchLayoutNodes:nodes ofKind:kind atIndexPaths:_pendingIndexPaths[kind] completion:nil];
_pendingNodes[kind] = nil; [_pendingNodes removeObjectForKey:kind];
_pendingIndexPaths[kind] = nil; [_pendingIndexPaths removeObjectForKey:kind];
}]; }];
} }
- (void)willMoveSection:(NSInteger)section toSection:(NSInteger)newSection - (void)willMoveSection:(NSInteger)section toSection:(NSInteger)newSection
{ {
[[self supplementaryKinds] enumerateObjectsUsingBlock:^(NSString *kind, NSUInteger idx, BOOL *stop) { NSArray *kinds = [self supplementaryKinds];
for (NSString *kind in kinds) {
NSArray *indexPaths = ASIndexPathsForMultidimensionalArrayAtIndexSet([self editingNodesOfKind:kind], [NSIndexSet indexSetWithIndex:section]); NSArray *indexPaths = ASIndexPathsForMultidimensionalArrayAtIndexSet([self editingNodesOfKind:kind], [NSIndexSet indexSetWithIndex:section]);
NSArray *nodes = ASFindElementsInMultidimensionalArrayAtIndexPaths([self editingNodesOfKind:kind], indexPaths); NSArray *nodes = ASFindElementsInMultidimensionalArrayAtIndexPaths([self editingNodesOfKind:kind], indexPaths);
[self deleteNodesOfKind:kind atIndexPaths:indexPaths completion:nil]; [self deleteNodesOfKind:kind atIndexPaths:indexPaths completion:nil];
@ -153,7 +167,7 @@
[updatedIndexPaths addObject:[sectionIndexPath indexPathByAddingIndex:[indexPath indexAtPosition:indexPath.length - 1]]]; [updatedIndexPaths addObject:[sectionIndexPath indexPathByAddingIndex:[indexPath indexAtPosition:indexPath.length - 1]]];
}]; }];
[self insertNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil]; [self insertNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil];
}]; }
} }
- (void)_populateSupplementaryNodesOfKind:(NSString *)kind withMutableNodes:(NSMutableArray *)nodes mutableIndexPaths:(NSMutableArray *)indexPaths - (void)_populateSupplementaryNodesOfKind:(NSString *)kind withMutableNodes:(NSMutableArray *)nodes mutableIndexPaths:(NSMutableArray *)indexPaths