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.
*
* @returns a 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). Unlike UICollectionView's version, this method
* @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
* is not called when the row is about to display.
*/
- (ASCellNode *)collectionView:(ASCollectionView *)collectionView nodeForItemAtIndexPath:(NSIndexPath *)indexPath;
@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.
*

View File

@@ -690,6 +690,33 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
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 constrainedSize = kInvalidSizeRange;

View File

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

View File

@@ -84,6 +84,17 @@
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
{
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.
*
* @returns a 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). Unlike UITableView's version, this method
* @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
* is not called when the row is about to display.
*/
- (ASCellNode *)tableView:(ASTableView *)tableView nodeForRowAtIndexPath:(NSIndexPath *)indexPath;
@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.
* 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;
}
- (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
{
return ASSizeRangeMake(CGSizeMake(_nodesConstrainedWidth, 0),

View File

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

View File

@@ -24,8 +24,8 @@
@end
@implementation ASCollectionDataController {
NSMutableDictionary *_pendingNodes;
NSMutableDictionary *_pendingIndexPaths;
NSMutableDictionary<NSString *, NSMutableArray<ASCellNode *> *> *_pendingNodes;
NSMutableDictionary<NSString *, NSMutableArray<NSIndexPath *> *> *_pendingIndexPaths;
}
- (instancetype)initWithAsyncDataFetching:(BOOL)asyncDataFetchingEnabled
@@ -49,7 +49,7 @@
_pendingIndexPaths[kind] = indexPaths;
// 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;
// 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;
// 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++) {
NSIndexPath *indexPath = [sectionIndexPath indexPathByAddingIndex:j];
[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++) {
NSIndexPath *indexPath = [sectionIndex indexPathByAddingIndex:i];
[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];
[nodes addObject:supplementaryNode];
supplementaryCellBlock = ^{ return supplementaryNode; };
}
[nodes addObject:supplementaryCellBlock];
}
}];
}

View File

@@ -20,12 +20,19 @@ NS_ASSUME_NONNULL_BEGIN
@class ASDataController;
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;
/**
Data source for data controller
It will be invoked in the same thread as the api call of ASDataController.
*/
@protocol ASDataControllerSource <NSObject>
/**
@@ -33,6 +40,11 @@ FOUNDATION_EXPORT NSString * const ASDataControllerRowNodeKind;
*/
- (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.
*/

View File

@@ -39,10 +39,13 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
NSOperationQueue *_editingTransactionQueue; // Serial background queue. Dispatches concurrent layout and manages _editingNodes.
BOOL _asyncDataFetchingEnabled;
BOOL _delegateDidInsertNodes;
BOOL _delegateDidDeleteNodes;
BOOL _delegateDidInsertSections;
BOOL _delegateDidDeleteSections;
BOOL _dataSourceImplementsAsyncNodeAtIndexPath;
}
@property (atomic, assign) NSUInteger batchUpdateCounter;
@@ -94,6 +97,16 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
_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
{
static NSUInteger parallelProcessorCount;
@@ -108,7 +121,7 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
#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;
@@ -117,12 +130,11 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
NSRange batchedRange = NSMakeRange(i, MIN(indexPaths.count - i, blockSize));
NSArray *batchedIndexPaths = [indexPaths subarrayWithRange:batchedRange];
NSArray *batchedNodes = [nodes subarrayWithRange:batchedRange];
[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.");
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, __unused BOOL * stop) {
@@ -160,21 +172,49 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
return;
}
NSUInteger nodeCount = nodes.count;
NSMutableArray<ASCellNode *> *allocatedNodes = [NSMutableArray arrayWithCapacity:nodeCount];
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) {
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++) {
ASCellNode *node = nodes[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]];
}
}
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), ^{
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.
// We should already have measured loaded nodes before we left the main thread, using layoutLoadedNodes:ofKind:atIndexPaths:
if (!node.isNodeLoaded) {
@@ -189,7 +229,7 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
free(nodeBoundSizes);
if (completionBlock) {
completionBlock(nodes, indexPaths);
completionBlock(allocatedNodes, indexPaths);
}
}
@@ -383,8 +423,10 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
NSMutableArray *updatedIndexPaths = [NSMutableArray array];
[self _populateFromEntireDataSourceWithMutableNodes:updatedNodes mutableIndexPaths:updatedIndexPaths];
// Measure nodes whose views are loaded before we leave the main thread
[self layoutLoadedNodes:updatedNodes ofKind:ASDataControllerRowNodeKind atIndexPaths:updatedIndexPaths];
// if (!_dataSourceImplementsAsyncNodeAtIndexPath) {
// // 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
[self prepareForReloadData];
@@ -412,6 +454,19 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
[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) {
dispatch_async(dispatch_get_main_queue(), completion);
}
@@ -462,13 +517,16 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
{
[indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
NSUInteger rowNum = [_dataSource dataController:self rowsInSection:idx];
NSIndexPath *sectionIndex = [[NSIndexPath alloc] initWithIndex:idx];
for (NSUInteger i = 0; i < rowNum; i++) {
NSIndexPath *indexPath = [sectionIndex indexPathByAddingIndex:i];
[indexPaths addObject:indexPath];
if (_dataSourceImplementsAsyncNodeAtIndexPath) {
[nodes addObject:[_dataSource dataController:self nodeBlockAtIndexPath:indexPath]];
} else {
[nodes addObject:[_dataSource dataController:self nodeAtIndexPath:indexPath]];
}
}
}];
}
@@ -482,15 +540,18 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
NSUInteger sectionNum = [_dataSource numberOfSectionsInDataController:self];
for (NSUInteger i = 0; i < sectionNum; i++) {
NSIndexPath *sectionIndexPath = [[NSIndexPath alloc] initWithIndex:i];
NSUInteger rowNum = [_dataSource dataController:self rowsInSection:i];
for (NSUInteger j = 0; j < rowNum; j++) {
NSIndexPath *indexPath = [sectionIndexPath indexPathByAddingIndex:j];
[indexPaths addObject:indexPath];
if (_dataSourceImplementsAsyncNodeAtIndexPath) {
[nodes addObject:[_dataSource dataController:self nodeBlockAtIndexPath:indexPath]];
} else {
[nodes addObject:[_dataSource dataController:self nodeAtIndexPath:indexPath]];
}
}
}
}
#pragma mark - Batching (External API)
@@ -578,8 +639,10 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
NSMutableArray *updatedIndexPaths = [NSMutableArray array];
[self _populateFromDataSourceWithSectionIndexSet:sections mutableNodes:updatedNodes mutableIndexPaths:updatedIndexPaths];
// Measure nodes whose views are loaded before we leave the main thread
[self layoutLoadedNodes:updatedNodes ofKind:ASDataControllerRowNodeKind atIndexPaths:updatedIndexPaths];
// if (!_dataSourceImplementsAsyncNodeAtIndexPath) {
// // Measure nodes whose views are loaded before we leave the main thread
// [self layoutLoadedNodes:updatedNodes ofKind:ASDataControllerRowNodeKind atIndexPaths:updatedIndexPaths];
// }
[self prepareForInsertSections:sections];
@@ -591,9 +654,22 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
for (NSUInteger i = 0; i < sections.count; i++) {
[sectionArray addObject:[NSMutableArray array]];
}
[self _insertSections:sectionArray atIndexSet:sections 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.
// 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[...]
// Measure nodes whose views are loaded before we leave the main thread
[self layoutLoadedNodes:updatedNodes ofKind:ASDataControllerRowNodeKind atIndexPaths:updatedIndexPaths];
// if (!_dataSourceImplementsAsyncNodeAtIndexPath) {
// // Measure nodes whose views are loaded before we leave the main thread
// [self layoutLoadedNodes:updatedNodes ofKind:ASDataControllerRowNodeKind atIndexPaths:updatedIndexPaths];
// }
[self prepareForReloadSections:sections];
@@ -652,6 +729,19 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
// reinsert the elements
[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:)];
NSMutableArray *nodes = [[NSMutableArray alloc] initWithCapacity:indexPaths.count];
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]]];
}
}
// 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:^{
LOG(@"Edit Transaction - insertRows: %@", indexPaths);
@@ -797,11 +891,15 @@ static void *kASSizingQueueContext = &kASSizingQueueContext;
[indexPaths sortedArrayUsingSelector:@selector(compare:)];
for (NSIndexPath *indexPath in indexPaths) {
if (_dataSourceImplementsAsyncNodeAtIndexPath) {
[nodes addObject:[_dataSource dataController:self nodeBlockAtIndexPath:indexPath]];
} else {
[nodes addObject:[_dataSource dataController:self nodeAtIndexPath:indexPath]];
}
}
// 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:^{
LOG(@"Edit Transaction - reloadRows: %@", indexPaths);

View File

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

View File

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

View File

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

View File

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