From e799ed9ea478e5223ae3206162e400fbb648ff84 Mon Sep 17 00:00:00 2001 From: Li Tan Date: Thu, 18 Dec 2014 13:58:44 -0800 Subject: [PATCH] fix --- AsyncDisplayKit/Details/ASDataController.mm | 38 ++++++++++++++----- .../Details/ASFlowLayoutController.mm | 3 +- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/AsyncDisplayKit/Details/ASDataController.mm b/AsyncDisplayKit/Details/ASDataController.mm index 5f6e2ee0ac..47489bce35 100644 --- a/AsyncDisplayKit/Details/ASDataController.mm +++ b/AsyncDisplayKit/Details/ASDataController.mm @@ -205,22 +205,40 @@ static void *kASDataUpdatingQueueContext = &kASDataUpdatingQueueContext; #pragma mark - Data Update - (void)insertSections:(NSIndexSet *)indexSet { - NSMutableArray *sectionArray = [[NSMutableArray alloc] initWithCapacity:indexSet.count]; - for (NSUInteger i = 0; i < indexSet.count; i++) { - NSUInteger rowNum = [_dataSource dataController:self rowsInSection:i]; - NSMutableArray *rows = [[NSMutableArray alloc] initWithCapacity:rowNum]; + __block int nodeTotalCnt = 0; + NSMutableArray *nodeCounts = [NSMutableArray arrayWithCapacity:indexSet.count]; + [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { + NSUInteger cnt = [_dataSource dataController:self rowsInSection:idx]; + [nodeCounts addObject:@(cnt)]; + nodeTotalCnt += cnt; + }]; - for (NSUInteger j = 0; j < rowNum; j++) { - ASCellNode *node = [_dataSource dataController:self nodeAtIndexPath:[NSIndexPath indexPathForItem:j inSection:i]]; - [rows addObject:node]; + NSMutableArray *nodes = [NSMutableArray arrayWithCapacity:nodeTotalCnt]; + NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:nodeTotalCnt]; + + __block NSUInteger idx = 0; + [indexSet enumerateIndexesUsingBlock:^(NSUInteger sectionIdx, BOOL *stop) { + NSUInteger cnt = [nodeCounts[idx++] unsignedIntegerValue]; + + for (int i = 0; i < cnt; i++) { + NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:sectionIdx]; + [indexPaths addObject:indexPath]; + + ASCellNode *node = [_dataSource dataController:self nodeAtIndexPath:indexPath]; + [nodes addObject:node]; } - [sectionArray addObject:rows]; - } + }]; dispatch_async([[self class] sizingQueue], ^{ - [self asyncUpdateDataWithBlock:^{ + [self syncUpdateDataWithBlock:^{ + NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:indexSet.count]; + for (NSUInteger i = 0; i < indexSet.count; i++) { + [sectionArray addObject:[NSMutableArray array]]; + } INSERT_SECTIONS(_nodes , indexSet, sectionArray); }]; + + [self _insertNodes:nodes atIndexPaths:indexPaths]; }); } diff --git a/AsyncDisplayKit/Details/ASFlowLayoutController.mm b/AsyncDisplayKit/Details/ASFlowLayoutController.mm index 91f35b91d6..27a932d68a 100644 --- a/AsyncDisplayKit/Details/ASFlowLayoutController.mm +++ b/AsyncDisplayKit/Details/ASFlowLayoutController.mm @@ -57,7 +57,8 @@ static const CGFloat kASFlowLayoutControllerRefreshingThreshold = 0.3; __block int cnt = 0; [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { NSArray *nodes = sections[cnt++]; - std::vector v(nodes.count); + std::vector v; + v.reserve(nodes.count); for (int i = 0; i < nodes.count; i++) { v.push_back([nodes[i] CGSizeValue]);