Move ASCellNode allocation off the main thread by the addition of a node block

API in ASDataController. Move allocations and loaded node layouts to occur
during batch layout phase.
This commit is contained in:
Rahul Malik
2016-02-02 15:00:24 -08:00
parent 35a4b268d6
commit 521c3fa1c1
14 changed files with 306 additions and 48 deletions

View File

@@ -347,14 +347,25 @@ NS_ASSUME_NONNULL_BEGIN
* *
* @param indexPath The index path of the requested node. * @param indexPath The index path of the requested node.
* *
* @returns a node for display at this indexpath. Must be thread-safe (can be called on the main thread or a background * @returns a node for display at this indexpath. This will be called on the main thread and should not implement reuse (it will be called once per row). Unlike UICollectionView's version, this method
* queue) and should not implement reuse (it will be called once per row). Unlike UICollectionView's version, this method
* is not called when the row is about to display. * is not called when the row is about to display.
*/ */
- (ASCellNode *)collectionView:(ASCollectionView *)collectionView nodeForItemAtIndexPath:(NSIndexPath *)indexPath; - (ASCellNode *)collectionView:(ASCollectionView *)collectionView nodeForItemAtIndexPath:(NSIndexPath *)indexPath;
@optional @optional
/**
*
* @param collectionView The sender.
*
* @param indexPath The index path of the requested node.
*
* @returns a block that creates the node for display at this indexpath. Must be thread-safe (can be called on the main thread or a background
* queue) and should not implement reuse (it will be called once per row).
*/
- (ASDataControllerCellNodeBlock)collectionView:(ASCollectionView *)collectionView nodeBlockAtIndexPath:(NSIndexPath *)indexPath;
/** /**
* Asks the collection view to provide a supplementary node to display in the collection view. * Asks the collection view to provide a supplementary node to display in the collection view.
* *

View File

@@ -690,6 +690,33 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
return node; return node;
} }
- (ASDataControllerCellNodeBlock)dataController:(ASDataController *)dataController nodeBlockAtIndexPath:(NSIndexPath *)indexPath
{
if (![_asyncDataSource respondsToSelector:@selector(collectionView:nodeBlockAtIndexPath:)]) {
ASCellNode *node = [_asyncDataSource collectionView:self nodeForItemAtIndexPath:indexPath];
return ^{
[node enterHierarchyState:ASHierarchyStateRangeManaged];
ASDisplayNodeAssert([node isKindOfClass:ASCellNode.class], @"invalid node class, expected ASCellNode");
if (node.layoutDelegate == nil) {
node.layoutDelegate = self;
}
return node;
};
}
ASDataControllerCellNodeBlock block = [_asyncDataSource collectionView:self nodeBlockAtIndexPath:indexPath];
ASDisplayNodeAssertNotNil(block, @"Invalid block, expected nonnull ASDataControllerCellNodeBlock");
return ^{
ASCellNode *node = block();
[node enterHierarchyState:ASHierarchyStateRangeManaged];
if (node.layoutDelegate == nil) {
node.layoutDelegate = self;
}
return node;
};
}
- (ASSizeRange)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath - (ASSizeRange)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
{ {
ASSizeRange constrainedSize = kInvalidSizeRange; ASSizeRange constrainedSize = kInvalidSizeRange;

View File

@@ -15,6 +15,12 @@
// This method replaces -collectionView:nodeForItemAtIndexPath: // This method replaces -collectionView:nodeForItemAtIndexPath:
- (ASCellNode *)pagerNode:(ASPagerNode *)pagerNode nodeAtIndex:(NSInteger)index; - (ASCellNode *)pagerNode:(ASPagerNode *)pagerNode nodeAtIndex:(NSInteger)index;
@optional
// This method replaces -collectionView:nodeBlockForItemAtIndexPath:
- (ASDataControllerCellNodeBlock)pagerNode:(ASPagerNode *)pagerNode nodeBlockAtIndex:(NSInteger)index;
@end @end
@interface ASPagerNode : ASCollectionNode @interface ASPagerNode : ASCollectionNode

View File

@@ -84,6 +84,17 @@
return pageNode; return pageNode;
} }
- (ASDataControllerCellNodeBlock)collectionView:(ASCollectionView *)collectionView nodeBlockAtIndexPath:(NSIndexPath *)indexPath {
ASDisplayNodeAssert(_pagerDataSource != nil, @"ASPagerNode must have a data source to load nodes to display");
if (![_pagerDataSource respondsToSelector:@selector(pagerNode:nodeBlockAtIndex:)]) {
ASCellNode *node = [_pagerDataSource pagerNode:self nodeAtIndex:indexPath.item];
return ^{ return node; };
}
ASDataControllerCellNodeBlock block = [_pagerDataSource pagerNode:self nodeBlockAtIndex:indexPath.item];
ASDisplayNodeAssertNotNil(block, @"Invalid node block. Block should be non-nil.");
return block;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{ {
ASDisplayNodeAssert(_pagerDataSource != nil, @"ASPagerNode must have a data source to load nodes to display"); ASDisplayNodeAssert(_pagerDataSource != nil, @"ASPagerNode must have a data source to load nodes to display");

View File

@@ -322,14 +322,26 @@ NS_ASSUME_NONNULL_BEGIN
* *
* @param indexPath The index path of the requested node. * @param indexPath The index path of the requested node.
* *
* @returns a node for display at this indexpath. Must be thread-safe (can be called on the main thread or a background * @returns a node for display at this indexpath. This will be called on the main thread and should not implement reuse (it will be called once per row). Unlike UITableView's version, this method
* queue) and should not implement reuse (it will be called once per row). Unlike UITableView's version, this method
* is not called when the row is about to display. * is not called when the row is about to display.
*/ */
- (ASCellNode *)tableView:(ASTableView *)tableView nodeForRowAtIndexPath:(NSIndexPath *)indexPath; - (ASCellNode *)tableView:(ASTableView *)tableView nodeForRowAtIndexPath:(NSIndexPath *)indexPath;
@optional @optional
/**
* Similar to -tableView:nodeForRowAtIndexPath:.
*
* @param tableView The sender.
*
* @param indexPath The index path of the requested node.
*
* @returns a block that creates the node for display at this indexpath. Must be thread-safe (can be called on the main thread or a background
* queue) and should not implement reuse (it will be called once per row).
*/
- (ASDataControllerCellNodeBlock)tableView:(ASTableView *)tableView nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath;
/** /**
* Indicator to lock the data source for data fetching in async mode. * Indicator to lock the data source for data fetching in async mode.
* We should not update the data source until the data source has been unlocked. Otherwise, it will incur data inconsistence or exception * We should not update the data source until the data source has been unlocked. Otherwise, it will incur data inconsistence or exception

View File

@@ -876,6 +876,33 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
return node; return node;
} }
- (ASDataControllerCellNodeBlock)dataController:(ASDataController *)dataController nodeBlockAtIndexPath:(NSIndexPath *)indexPath {
if (![_asyncDataSource respondsToSelector:@selector(tableView:nodeBlockForRowAtIndexPath:)]) {
ASCellNode *node = [_asyncDataSource tableView:self nodeForRowAtIndexPath:indexPath];
return ^{
[node enterHierarchyState:ASHierarchyStateRangeManaged];
ASDisplayNodeAssert([node isKindOfClass:ASCellNode.class], @"invalid node class, expected ASCellNode");
if (node.layoutDelegate == nil) {
node.layoutDelegate = self;
}
return node;
};
}
ASDataControllerCellNodeBlock block = [_asyncDataSource tableView:self nodeBlockForRowAtIndexPath:indexPath];
__weak __typeof__(self) weakSelf = self;
ASDataControllerCellNodeBlock configuredNodeBlock = ^{
__typeof__(self) strongSelf = weakSelf;
ASCellNode *node = block();
[node enterHierarchyState:ASHierarchyStateRangeManaged];
if (node.layoutDelegate == nil) {
node.layoutDelegate = strongSelf;
}
return node;
};
return configuredNodeBlock;
}
- (ASSizeRange)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath - (ASSizeRange)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
{ {
return ASSizeRangeMake(CGSizeMake(_nodesConstrainedWidth, 0), return ASSizeRangeMake(CGSizeMake(_nodesConstrainedWidth, 0),

View File

@@ -30,6 +30,10 @@
- (NSUInteger)dataController:(ASCollectionDataController *)dataController supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section; - (NSUInteger)dataController:(ASCollectionDataController *)dataController supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section;
@optional
- (ASDataControllerCellNodeBlock)dataController:(ASCollectionDataController *)dataController supplementaryNodeBlockOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
@end @end
@interface ASCollectionDataController : ASChangeSetDataController @interface ASCollectionDataController : ASChangeSetDataController

View File

@@ -24,8 +24,8 @@
@end @end
@implementation ASCollectionDataController { @implementation ASCollectionDataController {
NSMutableDictionary *_pendingNodes; NSMutableDictionary<NSString *, NSMutableArray<ASCellNode *> *> *_pendingNodes;
NSMutableDictionary *_pendingIndexPaths; NSMutableDictionary<NSString *, NSMutableArray<NSIndexPath *> *> *_pendingIndexPaths;
} }
- (instancetype)initWithAsyncDataFetching:(BOOL)asyncDataFetchingEnabled - (instancetype)initWithAsyncDataFetching:(BOOL)asyncDataFetchingEnabled
@@ -49,7 +49,7 @@
_pendingIndexPaths[kind] = indexPaths; _pendingIndexPaths[kind] = indexPaths;
// Measure loaded nodes before leaving the main thread // Measure loaded nodes before leaving the main thread
[self layoutLoadedNodes:nodes ofKind:kind atIndexPaths:indexPaths]; [self batchLayoutNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil];
} }
} }
@@ -91,7 +91,7 @@
_pendingIndexPaths[kind] = indexPaths; _pendingIndexPaths[kind] = indexPaths;
// Measure loaded nodes before leaving the main thread // Measure loaded nodes before leaving the main thread
[self layoutLoadedNodes:nodes ofKind:kind atIndexPaths:indexPaths]; [self batchLayoutNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil];
} }
} }
@@ -132,7 +132,7 @@
_pendingIndexPaths[kind] = indexPaths; _pendingIndexPaths[kind] = indexPaths;
// Measure loaded nodes before leaving the main thread // Measure loaded nodes before leaving the main thread
[self layoutLoadedNodes:nodes ofKind:kind atIndexPaths:indexPaths]; [self batchLayoutNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil];
} }
} }
@@ -176,7 +176,14 @@
for (NSUInteger j = 0; j < rowCount; j++) { for (NSUInteger j = 0; j < rowCount; j++) {
NSIndexPath *indexPath = [sectionIndexPath indexPathByAddingIndex:j]; NSIndexPath *indexPath = [sectionIndexPath indexPathByAddingIndex:j];
[indexPaths addObject:indexPath]; [indexPaths addObject:indexPath];
[nodes addObject:[self.collectionDataSource dataController:self supplementaryNodeOfKind:kind atIndexPath:indexPath]]; ASDataControllerCellNodeBlock supplementaryCellBlock;
if ([self.collectionDataSource respondsToSelector:@selector(dataController:supplementaryNodeBlockOfKind:atIndexPath:)]) {
supplementaryCellBlock = [self.collectionDataSource dataController:self supplementaryNodeBlockOfKind:kind atIndexPath:indexPath];
} else {
ASCellNode *supplementaryNode = [self.collectionDataSource dataController:self supplementaryNodeOfKind:kind atIndexPath:indexPath];
supplementaryCellBlock = ^{ return supplementaryNode; };
}
[nodes addObject:supplementaryCellBlock];
} }
} }
} }
@@ -189,8 +196,14 @@
for (NSUInteger i = 0; i < rowNum; i++) { for (NSUInteger i = 0; i < rowNum; i++) {
NSIndexPath *indexPath = [sectionIndex indexPathByAddingIndex:i]; NSIndexPath *indexPath = [sectionIndex indexPathByAddingIndex:i];
[indexPaths addObject:indexPath]; [indexPaths addObject:indexPath];
ASDataControllerCellNodeBlock supplementaryCellBlock;
if ([self.collectionDataSource respondsToSelector:@selector(dataController:supplementaryNodeBlockOfKind:atIndexPath:)]) {
supplementaryCellBlock = [self.collectionDataSource dataController:self supplementaryNodeBlockOfKind:kind atIndexPath:indexPath];
} else {
ASCellNode *supplementaryNode = [self.collectionDataSource dataController:self supplementaryNodeOfKind:kind atIndexPath:indexPath]; ASCellNode *supplementaryNode = [self.collectionDataSource dataController:self supplementaryNodeOfKind:kind atIndexPath:indexPath];
[nodes addObject:supplementaryNode]; supplementaryCellBlock = ^{ return supplementaryNode; };
}
[nodes addObject:supplementaryCellBlock];
} }
}]; }];
} }

View File

@@ -20,12 +20,19 @@ NS_ASSUME_NONNULL_BEGIN
@class ASDataController; @class ASDataController;
typedef NSUInteger ASDataControllerAnimationOptions; typedef NSUInteger ASDataControllerAnimationOptions;
/**
* ASCellNode creation block. Used to lazily create the ASCellNode instance for a specified indexPath.
*/
typedef ASCellNode * _Nonnull(^ASDataControllerCellNodeBlock)();
FOUNDATION_EXPORT NSString * const ASDataControllerRowNodeKind; FOUNDATION_EXPORT NSString * const ASDataControllerRowNodeKind;
/** /**
Data source for data controller Data source for data controller
It will be invoked in the same thread as the api call of ASDataController. It will be invoked in the same thread as the api call of ASDataController.
*/ */
@protocol ASDataControllerSource <NSObject> @protocol ASDataControllerSource <NSObject>
/** /**
@@ -33,6 +40,11 @@ FOUNDATION_EXPORT NSString * const ASDataControllerRowNodeKind;
*/ */
- (ASCellNode *)dataController:(ASDataController *)dataController nodeAtIndexPath:(NSIndexPath *)indexPath; - (ASCellNode *)dataController:(ASDataController *)dataController nodeAtIndexPath:(NSIndexPath *)indexPath;
/**
Fetch the ASCellNode block for specific index path. This block should return the ASCellNode for the specified index path.
*/
- (ASDataControllerCellNodeBlock)dataController:(ASDataController *)dataController nodeBlockAtIndexPath:(NSIndexPath *)indexPath;
/** /**
The constrained size range for layout. The constrained size range for layout.
*/ */

View File

@@ -39,10 +39,13 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
NSOperationQueue *_editingTransactionQueue; // Serial background queue. Dispatches concurrent layout and manages _editingNodes. NSOperationQueue *_editingTransactionQueue; // Serial background queue. Dispatches concurrent layout and manages _editingNodes.
BOOL _asyncDataFetchingEnabled; BOOL _asyncDataFetchingEnabled;
BOOL _delegateDidInsertNodes; BOOL _delegateDidInsertNodes;
BOOL _delegateDidDeleteNodes; BOOL _delegateDidDeleteNodes;
BOOL _delegateDidInsertSections; BOOL _delegateDidInsertSections;
BOOL _delegateDidDeleteSections; BOOL _delegateDidDeleteSections;
BOOL _dataSourceImplementsAsyncNodeAtIndexPath;
} }
@property (atomic, assign) NSUInteger batchUpdateCounter; @property (atomic, assign) NSUInteger batchUpdateCounter;
@@ -94,6 +97,16 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
_delegateDidDeleteSections = [_delegate respondsToSelector:@selector(dataController:didDeleteSectionsAtIndexSet:withAnimationOptions:)]; _delegateDidDeleteSections = [_delegate respondsToSelector:@selector(dataController:didDeleteSectionsAtIndexSet:withAnimationOptions:)];
} }
- (void)setDataSource:(id<ASDataControllerSource>)dataSource {
if (_dataSource == dataSource) {
return;
}
_dataSource = dataSource;
// This probably won't be sufficient to tell if we should call the node block
_dataSourceImplementsAsyncNodeAtIndexPath = [_dataSource respondsToSelector:@selector(dataController:nodeBlockAtIndexPath:)];
}
+ (NSUInteger)parallelProcessorCount + (NSUInteger)parallelProcessorCount
{ {
static NSUInteger parallelProcessorCount; static NSUInteger parallelProcessorCount;
@@ -108,7 +121,7 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
#pragma mark - Cell Layout #pragma mark - Cell Layout
- (void)batchLayoutNodes:(NSArray *)nodes ofKind:(NSString *)kind atIndexPaths:(NSArray *)indexPaths completion:(void (^)(NSArray *nodes, NSArray *indexPaths))completionBlock - (void)batchLayoutNodes:(NSArray<ASDataControllerCellNodeBlock> *)nodes ofKind:(NSString *)kind atIndexPaths:(NSArray<NSIndexPath *> *)indexPaths completion:(void (^)(NSArray<ASCellNode *> *nodes, NSArray<NSIndexPath *> *indexPaths))completionBlock
{ {
NSUInteger blockSize = [[ASDataController class] parallelProcessorCount] * kASDataControllerSizingCountPerProcessor; NSUInteger blockSize = [[ASDataController class] parallelProcessorCount] * kASDataControllerSizingCountPerProcessor;
@@ -117,12 +130,11 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
NSRange batchedRange = NSMakeRange(i, MIN(indexPaths.count - i, blockSize)); NSRange batchedRange = NSMakeRange(i, MIN(indexPaths.count - i, blockSize));
NSArray *batchedIndexPaths = [indexPaths subarrayWithRange:batchedRange]; NSArray *batchedIndexPaths = [indexPaths subarrayWithRange:batchedRange];
NSArray *batchedNodes = [nodes subarrayWithRange:batchedRange]; NSArray *batchedNodes = [nodes subarrayWithRange:batchedRange];
[self _layoutNodes:batchedNodes ofKind:kind atIndexPaths:batchedIndexPaths completion:completionBlock]; [self _layoutNodes:batchedNodes ofKind:kind atIndexPaths:batchedIndexPaths completion:completionBlock];
} }
} }
- (void)layoutLoadedNodes:(NSArray *)nodes ofKind:(NSString *)kind atIndexPaths:(NSArray *)indexPaths { - (void)layoutLoadedNodes:(NSArray<ASCellNode *> *)nodes ofKind:(NSString *)kind atIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
NSAssert(NSThread.isMainThread, @"Main thread layout must be on the main thread."); NSAssert(NSThread.isMainThread, @"Main thread layout must be on the main thread.");
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, __unused BOOL * stop) { [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, __unused BOOL * stop) {
@@ -160,21 +172,49 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
return; return;
} }
NSUInteger nodeCount = nodes.count;
NSMutableArray<ASCellNode *> *allocatedNodes = [NSMutableArray arrayWithCapacity:nodeCount];
dispatch_group_t layoutGroup = dispatch_group_create(); dispatch_group_t layoutGroup = dispatch_group_create();
ASSizeRange *nodeBoundSizes = (ASSizeRange *)malloc(sizeof(ASSizeRange) * nodes.count); ASSizeRange *nodeBoundSizes = (ASSizeRange *)malloc(sizeof(ASSizeRange) * nodeCount);
BOOL isMainThread = [NSThread isMainThread];
for (NSUInteger j = 0; j < nodes.count && j < indexPaths.count; j += kASDataControllerSizingCountPerProcessor) { for (NSUInteger j = 0; j < nodes.count && j < indexPaths.count; j += kASDataControllerSizingCountPerProcessor) {
NSInteger batchCount = MIN(kASDataControllerSizingCountPerProcessor, indexPaths.count - j); NSInteger batchCount = MIN(kASDataControllerSizingCountPerProcessor, indexPaths.count - j);
if (isMainThread) {
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (NSUInteger k = j; k < j + batchCount; k++) { for (NSUInteger k = j; k < j + batchCount; k++) {
ASCellNode *node = nodes[k]; ASDataControllerCellNodeBlock cellBlock = nodes[k];
ASCellNode *node = cellBlock();
ASDisplayNodeAssertNotNil(node, @"Node block created nil node");
[allocatedNodes addObject:node];
if (!node.isNodeLoaded) { if (!node.isNodeLoaded) {
nodeBoundSizes[k] = [self constrainedSizeForNodeOfKind:kind atIndexPath:indexPaths[k]]; nodeBoundSizes[k] = [self constrainedSizeForNodeOfKind:kind atIndexPath:indexPaths[k]];
} }
} }
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
[self layoutLoadedNodes:allocatedNodes ofKind:kind atIndexPaths:[indexPaths subarrayWithRange:NSMakeRange(j, batchCount)]];
} else {
for (NSUInteger k = j; k < j + batchCount; k++) {
ASDataControllerCellNodeBlock cellBlock = nodes[k];
ASCellNode *node = cellBlock();
ASDisplayNodeAssertNotNil(node, @"Node block created nil node");
[allocatedNodes addObject:node];
if (!node.isNodeLoaded) {
nodeBoundSizes[k] = [self constrainedSizeForNodeOfKind:kind atIndexPath:indexPaths[k]];
}
}
[_mainSerialQueue performBlockOnMainThread:^{
[self layoutLoadedNodes:allocatedNodes ofKind:kind atIndexPaths:[indexPaths subarrayWithRange:NSMakeRange(j, batchCount)]];
}];
}
dispatch_group_async(layoutGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_group_async(layoutGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (NSUInteger k = j; k < j + batchCount; k++) { for (NSUInteger k = j; k < j + batchCount; k++) {
ASCellNode *node = nodes[k]; ASCellNode *node = allocatedNodes[k];
// Only measure nodes whose views aren't loaded, since we're in the background. // Only measure nodes whose views aren't loaded, since we're in the background.
// We should already have measured loaded nodes before we left the main thread, using layoutLoadedNodes:ofKind:atIndexPaths: // We should already have measured loaded nodes before we left the main thread, using layoutLoadedNodes:ofKind:atIndexPaths:
if (!node.isNodeLoaded) { if (!node.isNodeLoaded) {
@@ -189,7 +229,7 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
free(nodeBoundSizes); free(nodeBoundSizes);
if (completionBlock) { if (completionBlock) {
completionBlock(nodes, indexPaths); completionBlock(allocatedNodes, indexPaths);
} }
} }
@@ -383,8 +423,10 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
NSMutableArray *updatedIndexPaths = [NSMutableArray array]; NSMutableArray *updatedIndexPaths = [NSMutableArray array];
[self _populateFromEntireDataSourceWithMutableNodes:updatedNodes mutableIndexPaths:updatedIndexPaths]; [self _populateFromEntireDataSourceWithMutableNodes:updatedNodes mutableIndexPaths:updatedIndexPaths];
// Measure nodes whose views are loaded before we leave the main thread // if (!_dataSourceImplementsAsyncNodeAtIndexPath) {
[self layoutLoadedNodes:updatedNodes ofKind:ASDataControllerRowNodeKind atIndexPaths:updatedIndexPaths]; // // Measure nodes whose views are loaded before we leave the main thread
// [self layoutLoadedNodes:updatedNodes ofKind:ASDataControllerRowNodeKind atIndexPaths:updatedIndexPaths];
// }
// Allow subclasses to perform setup before going into the edit transaction // Allow subclasses to perform setup before going into the edit transaction
[self prepareForReloadData]; [self prepareForReloadData];
@@ -412,6 +454,19 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
[self _batchLayoutNodes:updatedNodes atIndexPaths:updatedIndexPaths withAnimationOptions:animationOptions]; [self _batchLayoutNodes:updatedNodes atIndexPaths:updatedIndexPaths withAnimationOptions:animationOptions];
// if (_dataSourceImplementsAsyncNodeAtIndexPath) {
// NSMutableArray *allocatedNodes = [NSMutableArray arrayWithCapacity:updatedNodes.count];
// for (NSUInteger i = 0; i < updatedNodes.count; i++) {
// ASDataControllerCellNodeBlock cellCreationBlock = updatedNodes[i];
// __kindof ASCellNode *cellNode = cellCreationBlock();
// ASDisplayNodeAssertFalse(cellNode.isNodeLoaded);
// [allocatedNodes addObject:cellNode];
// }
// [self _batchLayoutNodes:allocatedNodes atIndexPaths:updatedIndexPaths withAnimationOptions:animationOptions];
// } else {
// [self _batchLayoutNodes:updatedNodes atIndexPaths:updatedIndexPaths withAnimationOptions:animationOptions];
// }
if (completion) { if (completion) {
dispatch_async(dispatch_get_main_queue(), completion); dispatch_async(dispatch_get_main_queue(), completion);
} }
@@ -462,13 +517,16 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
{ {
[indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
NSUInteger rowNum = [_dataSource dataController:self rowsInSection:idx]; NSUInteger rowNum = [_dataSource dataController:self rowsInSection:idx];
NSIndexPath *sectionIndex = [[NSIndexPath alloc] initWithIndex:idx]; NSIndexPath *sectionIndex = [[NSIndexPath alloc] initWithIndex:idx];
for (NSUInteger i = 0; i < rowNum; i++) { for (NSUInteger i = 0; i < rowNum; i++) {
NSIndexPath *indexPath = [sectionIndex indexPathByAddingIndex:i]; NSIndexPath *indexPath = [sectionIndex indexPathByAddingIndex:i];
[indexPaths addObject:indexPath]; [indexPaths addObject:indexPath];
if (_dataSourceImplementsAsyncNodeAtIndexPath) {
[nodes addObject:[_dataSource dataController:self nodeBlockAtIndexPath:indexPath]];
} else {
[nodes addObject:[_dataSource dataController:self nodeAtIndexPath:indexPath]]; [nodes addObject:[_dataSource dataController:self nodeAtIndexPath:indexPath]];
} }
}
}]; }];
} }
@@ -482,14 +540,17 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
NSUInteger sectionNum = [_dataSource numberOfSectionsInDataController:self]; NSUInteger sectionNum = [_dataSource numberOfSectionsInDataController:self];
for (NSUInteger i = 0; i < sectionNum; i++) { for (NSUInteger i = 0; i < sectionNum; i++) {
NSIndexPath *sectionIndexPath = [[NSIndexPath alloc] initWithIndex:i]; NSIndexPath *sectionIndexPath = [[NSIndexPath alloc] initWithIndex:i];
NSUInteger rowNum = [_dataSource dataController:self rowsInSection:i]; NSUInteger rowNum = [_dataSource dataController:self rowsInSection:i];
for (NSUInteger j = 0; j < rowNum; j++) { for (NSUInteger j = 0; j < rowNum; j++) {
NSIndexPath *indexPath = [sectionIndexPath indexPathByAddingIndex:j]; NSIndexPath *indexPath = [sectionIndexPath indexPathByAddingIndex:j];
[indexPaths addObject:indexPath]; [indexPaths addObject:indexPath];
if (_dataSourceImplementsAsyncNodeAtIndexPath) {
[nodes addObject:[_dataSource dataController:self nodeBlockAtIndexPath:indexPath]];
} else {
[nodes addObject:[_dataSource dataController:self nodeAtIndexPath:indexPath]]; [nodes addObject:[_dataSource dataController:self nodeAtIndexPath:indexPath]];
} }
} }
}
} }
@@ -578,8 +639,10 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
NSMutableArray *updatedIndexPaths = [NSMutableArray array]; NSMutableArray *updatedIndexPaths = [NSMutableArray array];
[self _populateFromDataSourceWithSectionIndexSet:sections mutableNodes:updatedNodes mutableIndexPaths:updatedIndexPaths]; [self _populateFromDataSourceWithSectionIndexSet:sections mutableNodes:updatedNodes mutableIndexPaths:updatedIndexPaths];
// Measure nodes whose views are loaded before we leave the main thread // if (!_dataSourceImplementsAsyncNodeAtIndexPath) {
[self layoutLoadedNodes:updatedNodes ofKind:ASDataControllerRowNodeKind atIndexPaths:updatedIndexPaths]; // // Measure nodes whose views are loaded before we leave the main thread
// [self layoutLoadedNodes:updatedNodes ofKind:ASDataControllerRowNodeKind atIndexPaths:updatedIndexPaths];
// }
[self prepareForInsertSections:sections]; [self prepareForInsertSections:sections];
@@ -591,9 +654,22 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
for (NSUInteger i = 0; i < sections.count; i++) { for (NSUInteger i = 0; i < sections.count; i++) {
[sectionArray addObject:[NSMutableArray array]]; [sectionArray addObject:[NSMutableArray array]];
} }
[self _insertSections:sectionArray atIndexSet:sections withAnimationOptions:animationOptions]; [self _insertSections:sectionArray atIndexSet:sections withAnimationOptions:animationOptions];
[self _batchLayoutNodes:updatedNodes atIndexPaths:updatedIndexPaths withAnimationOptions:animationOptions]; [self _batchLayoutNodes:updatedNodes atIndexPaths:updatedIndexPaths withAnimationOptions:animationOptions];
// if (_dataSourceImplementsAsyncNodeAtIndexPath) {
// NSMutableArray *allocatedNodes = [NSMutableArray arrayWithCapacity:updatedNodes.count];
// for (NSUInteger i = 0; i < updatedNodes.count; i++) {
// ASDataControllerCellNodeBlock cellCreationBlock = updatedNodes[i];
// __kindof ASCellNode *cellNode = cellCreationBlock();
// ASDisplayNodeAssertFalse(cellNode.isNodeLoaded);
// [allocatedNodes addObject:cellNode];
// }
// [self _batchLayoutNodes:allocatedNodes atIndexPaths:updatedIndexPaths withAnimationOptions:animationOptions];
// } else {
// [self _batchLayoutNodes:updatedNodes atIndexPaths:updatedIndexPaths withAnimationOptions:animationOptions];
// }
}]; }];
}]; }];
}]; }];
@@ -635,9 +711,10 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
// Dispatch to sizing queue in order to guarantee that any in-progress sizing operations from prior edits have completed. // Dispatch to sizing queue in order to guarantee that any in-progress sizing operations from prior edits have completed.
// For example, if an initial -reloadData call is quickly followed by -reloadSections, sizing the initial set may not be done // For example, if an initial -reloadData call is quickly followed by -reloadSections, sizing the initial set may not be done
// at this time. Thus _editingNodes could be empty and crash in ASIndexPathsForMultidimensional[...] // at this time. Thus _editingNodes could be empty and crash in ASIndexPathsForMultidimensional[...]
// if (!_dataSourceImplementsAsyncNodeAtIndexPath) {
// Measure nodes whose views are loaded before we leave the main thread // // Measure nodes whose views are loaded before we leave the main thread
[self layoutLoadedNodes:updatedNodes ofKind:ASDataControllerRowNodeKind atIndexPaths:updatedIndexPaths]; // [self layoutLoadedNodes:updatedNodes ofKind:ASDataControllerRowNodeKind atIndexPaths:updatedIndexPaths];
// }
[self prepareForReloadSections:sections]; [self prepareForReloadSections:sections];
@@ -652,6 +729,19 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
// reinsert the elements // reinsert the elements
[self _batchLayoutNodes:updatedNodes atIndexPaths:updatedIndexPaths withAnimationOptions:animationOptions]; [self _batchLayoutNodes:updatedNodes atIndexPaths:updatedIndexPaths withAnimationOptions:animationOptions];
// if (_dataSourceImplementsAsyncNodeAtIndexPath) {
// NSMutableArray *allocatedNodes = [NSMutableArray arrayWithCapacity:updatedNodes.count];
// for (NSUInteger i = 0; i < updatedNodes.count; i++) {
// ASDataControllerCellNodeBlock cellCreationBlock = updatedNodes[i];
// __kindof ASCellNode *cellNode = cellCreationBlock();
// ASDisplayNodeAssertFalse(cellNode.isNodeLoaded);
// [allocatedNodes addObject:cellNode];
// }
// [self _batchLayoutNodes:allocatedNodes atIndexPaths:updatedIndexPaths withAnimationOptions:animationOptions];
// } else {
// [self _batchLayoutNodes:updatedNodes atIndexPaths:updatedIndexPaths withAnimationOptions:animationOptions];
// }
}]; }];
}]; }];
}]; }];
@@ -747,11 +837,15 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
NSArray *sortedIndexPaths = [indexPaths sortedArrayUsingSelector:@selector(compare:)]; NSArray *sortedIndexPaths = [indexPaths sortedArrayUsingSelector:@selector(compare:)];
NSMutableArray *nodes = [[NSMutableArray alloc] initWithCapacity:indexPaths.count]; NSMutableArray *nodes = [[NSMutableArray alloc] initWithCapacity:indexPaths.count];
for (NSUInteger i = 0; i < sortedIndexPaths.count; i++) { for (NSUInteger i = 0; i < sortedIndexPaths.count; i++) {
if (_dataSourceImplementsAsyncNodeAtIndexPath) {
[nodes addObject:[_dataSource dataController:self nodeBlockAtIndexPath:sortedIndexPaths[i]]];
} else {
[nodes addObject:[_dataSource dataController:self nodeAtIndexPath:sortedIndexPaths[i]]]; [nodes addObject:[_dataSource dataController:self nodeAtIndexPath:sortedIndexPaths[i]]];
} }
}
// Measure nodes whose views are loaded before we leave the main thread // Measure nodes whose views are loaded before we leave the main thread
[self layoutLoadedNodes:nodes ofKind:ASDataControllerRowNodeKind atIndexPaths:indexPaths]; // [self layoutLoadedNodes:nodes ofKind:ASDataControllerRowNodeKind atIndexPaths:indexPaths];
[_editingTransactionQueue addOperationWithBlock:^{ [_editingTransactionQueue addOperationWithBlock:^{
LOG(@"Edit Transaction - insertRows: %@", indexPaths); LOG(@"Edit Transaction - insertRows: %@", indexPaths);
@@ -797,11 +891,15 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
[indexPaths sortedArrayUsingSelector:@selector(compare:)]; [indexPaths sortedArrayUsingSelector:@selector(compare:)];
for (NSIndexPath *indexPath in indexPaths) { for (NSIndexPath *indexPath in indexPaths) {
if (_dataSourceImplementsAsyncNodeAtIndexPath) {
[nodes addObject:[_dataSource dataController:self nodeBlockAtIndexPath:indexPath]];
} else {
[nodes addObject:[_dataSource dataController:self nodeAtIndexPath:indexPath]]; [nodes addObject:[_dataSource dataController:self nodeAtIndexPath:indexPath]];
} }
}
// Measure nodes whose views are loaded before we leave the main thread // Measure nodes whose views are loaded before we leave the main thread
[self layoutLoadedNodes:nodes ofKind:ASDataControllerRowNodeKind atIndexPaths:indexPaths]; // [self layoutLoadedNodes:nodes ofKind:ASDataControllerRowNodeKind atIndexPaths:indexPaths];
[_editingTransactionQueue addOperationWithBlock:^{ [_editingTransactionQueue addOperationWithBlock:^{
LOG(@"Edit Transaction - reloadRows: %@", indexPaths); LOG(@"Edit Transaction - reloadRows: %@", indexPaths);

View File

@@ -81,8 +81,9 @@
@end @end
@implementation ASDelegateProxy { @implementation ASDelegateProxy {
id <NSObject> __weak _target;
id <ASDelegateProxyInterceptor> __weak _interceptor; id <ASDelegateProxyInterceptor> __weak _interceptor;
@protected
NSObject * __weak _target;
} }
- (instancetype)initWithTarget:(id <NSObject>)target interceptor:(id <ASDelegateProxyInterceptor>)interceptor - (instancetype)initWithTarget:(id <NSObject>)target interceptor:(id <ASDelegateProxyInterceptor>)interceptor
@@ -130,4 +131,12 @@
return NO; return NO;
} }
- (nullable NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
if ([_target isEqual:[NSNull null]]) {
return nil;
}
return [_target methodSignatureForSelector:selector];
}
@end @end

View File

@@ -25,6 +25,10 @@
return [[ASCellNode alloc] init]; return [[ASCellNode alloc] init];
} }
- (ASDataControllerCellNodeBlock)collectionView:(ASCollectionView *)collectionView nodeBlockAtIndexPath:(NSIndexPath *)indexPath {
return ^{ return [[ASCellNode alloc] init]; };
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{ {
return 0; return 0;

View File

@@ -35,6 +35,15 @@
return textCellNode; return textCellNode;
} }
- (ASDataControllerCellNodeBlock)collectionView:(ASCollectionView *)collectionView nodeBlockAtIndexPath:(NSIndexPath *)indexPath {
return ^{
ASTextCellNode *textCellNode = [ASTextCellNode new];
textCellNode.text = indexPath.description;
return textCellNode;
};
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return self.numberOfSections; return self.numberOfSections;
} }

View File

@@ -72,6 +72,11 @@
return nil; return nil;
} }
- (ASDataControllerCellNodeBlock)tableView:(ASTableView *)tableView nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath
{
return nil;
}
- (void)dealloc - (void)dealloc
{ {
if (_willDeallocBlock) { if (_willDeallocBlock) {
@@ -121,6 +126,16 @@
return textCellNode; return textCellNode;
} }
- (ASDataControllerCellNodeBlock)tableView:(ASTableView *)tableView nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ^{
ASTestTextCellNode *textCellNode = [ASTestTextCellNode new];
textCellNode.text = indexPath.description;
return textCellNode;
};
}
@end @end
@interface ASTableViewTests : XCTestCase @interface ASTableViewTests : XCTestCase