From 41a53887d74dbb3173db8ca09c4fc8cb96e8b2d3 Mon Sep 17 00:00:00 2001 From: Henish Shah Date: Fri, 13 May 2016 11:51:01 -0400 Subject: [PATCH] [Sample] Resolve mutation during enumeration issue - Mutating a dictionary using -enumerateKeysAndObjectsUsingBlock: can have unintended consequences. - Using a copy of the keys to iterate over the values inside the dictionary instead. --- .../Details/ASCollectionDataController.mm | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/AsyncDisplayKit/Details/ASCollectionDataController.mm b/AsyncDisplayKit/Details/ASCollectionDataController.mm index b264102a9f..ba1647a282 100644 --- a/AsyncDisplayKit/Details/ASCollectionDataController.mm +++ b/AsyncDisplayKit/Details/ASCollectionDataController.mm @@ -89,7 +89,9 @@ - (void)willInsertSections:(NSIndexSet *)sections { - [_pendingContexts enumerateKeysAndObjectsUsingBlock:^(NSString *kind, NSMutableArray *contexts, BOOL *stop) { + NSArray *keys = _pendingContexts.allKeys; + for (NSString *kind in keys) { + NSMutableArray *contexts = _pendingContexts[kind]; NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:sections.count]; for (NSUInteger i = 0; i < sections.count; i++) { [sectionArray addObject:[NSMutableArray array]]; @@ -100,7 +102,7 @@ [self insertNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil]; }]; [_pendingContexts removeObjectForKey:kind]; - }]; + } } - (void)willDeleteSections:(NSIndexSet *)sections @@ -124,7 +126,9 @@ - (void)willReloadSections:(NSIndexSet *)sections { - [_pendingContexts enumerateKeysAndObjectsUsingBlock:^(NSString *kind, NSMutableArray *contexts, BOOL *stop) { + NSArray *keys = _pendingContexts.allKeys; + for (NSString *kind in keys) { + NSMutableArray *contexts = _pendingContexts[kind]; NSArray *indexPaths = ASIndexPathsForMultidimensionalArrayAtIndexSet([self editingNodesOfKind:kind], sections); [self deleteNodesOfKind:kind atIndexPaths:indexPaths completion:nil]; // reinsert the elements @@ -132,7 +136,7 @@ [self insertNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil]; }]; [_pendingContexts removeObjectForKey:kind]; - }]; + } } - (void)willMoveSection:(NSInteger)section toSection:(NSInteger)newSection