1313#import " ASAssert.h"
1414#import " ASCellNode.h"
1515#import " ASDisplayNode.h"
16+ #import " ASMainSerialQueue.h"
1617#import " ASMultidimensionalArrayUtils.h"
1718#import " ASInternalHelpers.h"
1819#import " ASLayout.h"
@@ -31,7 +32,7 @@ @interface ASDataController () {
3132 NSMutableDictionary *_completedNodes; // Main thread only. External data access can immediately query this if _externalCompletedNodes is unavailable.
3233 NSMutableDictionary *_editingNodes; // Modified on _editingTransactionQueue only. Updates propogated to _completedNodes.
3334
34-
35+ ASMainSerialQueue *_mainSerialQueue;
3536
3637 NSMutableArray *_pendingEditCommandBlocks; // To be run on the main thread. Handles begin/endUpdates tracking.
3738 NSOperationQueue *_editingTransactionQueue; // Serial background queue. Dispatches concurrent layout and manages _editingNodes.
@@ -63,6 +64,8 @@ - (instancetype)initWithAsyncDataFetching:(BOOL)asyncDataFetchingEnabled
6364 _completedNodes[ASDataControllerRowNodeKind] = [NSMutableArray array ];
6465 _editingNodes[ASDataControllerRowNodeKind] = [NSMutableArray array ];
6566
67+ _mainSerialQueue = [[ASMainSerialQueue alloc ] init ];
68+
6669 _pendingEditCommandBlocks = [NSMutableArray array ];
6770
6871 _editingTransactionQueue = [[NSOperationQueue alloc ] init ];
@@ -208,12 +211,12 @@ - (void)insertNodes:(NSArray *)nodes ofKind:(NSString *)kind atIndexPaths:(NSArr
208211 // Deep copy is critical here, or future edits to the sub-arrays will pollute state between _editing and _complete on different threads.
209212 NSMutableArray *completedNodes = (NSMutableArray *)ASMultidimensionalArrayDeepMutableCopy (editingNodes);
210213
211- ASPerformBlockOnMainThread ( ^{
214+ [_mainSerialQueue performBlockOnMainThread: ^{
212215 _completedNodes[kind] = completedNodes;
213216 if (completionBlock) {
214217 completionBlock (nodes, indexPaths);
215218 }
216- }) ;
219+ }] ;
217220}
218221
219222- (void )deleteNodesOfKind : (NSString *)kind atIndexPaths : (NSArray *)indexPaths completion : (void (^)(NSArray *nodes, NSArray *indexPaths))completionBlock
@@ -227,13 +230,13 @@ - (void)deleteNodesOfKind:(NSString *)kind atIndexPaths:(NSArray *)indexPaths co
227230 ASDeleteElementsInMultidimensionalArrayAtIndexPaths (editingNodes, indexPaths);
228231 _editingNodes[kind] = editingNodes;
229232
230- ASPerformBlockOnMainThread ( ^{
233+ [_mainSerialQueue performBlockOnMainThread: ^{
231234 NSArray *nodes = ASFindElementsInMultidimensionalArrayAtIndexPaths (_completedNodes[kind], indexPaths);
232235 ASDeleteElementsInMultidimensionalArrayAtIndexPaths (_completedNodes[kind], indexPaths);
233236 if (completionBlock) {
234237 completionBlock (nodes, indexPaths);
235238 }
236- }) ;
239+ }] ;
237240}
238241
239242- (void )insertSections : (NSMutableArray *)sections ofKind : (NSString *)kind atIndexSet : (NSIndexSet *)indexSet completion : (void (^)(NSArray *sections, NSIndexSet *indexSet))completionBlock
@@ -250,25 +253,25 @@ - (void)insertSections:(NSMutableArray *)sections ofKind:(NSString *)kind atInde
250253 // Deep copy is critical here, or future edits to the sub-arrays will pollute state between _editing and _complete on different threads.
251254 NSArray *sectionsForCompleted = (NSMutableArray *)ASMultidimensionalArrayDeepMutableCopy (sections);
252255
253- ASPerformBlockOnMainThread ( ^{
256+ [_mainSerialQueue performBlockOnMainThread: ^{
254257 [_completedNodes[kind] insertObjects: sectionsForCompleted atIndexes: indexSet];
255258 if (completionBlock) {
256259 completionBlock (sections, indexSet);
257260 }
258- }) ;
261+ }] ;
259262}
260263
261264- (void )deleteSectionsOfKind : (NSString *)kind atIndexSet : (NSIndexSet *)indexSet completion : (void (^)(NSIndexSet *indexSet))completionBlock
262265{
263266 if (indexSet.count == 0 )
264267 return ;
265268 [_editingNodes[kind] removeObjectsAtIndexes: indexSet];
266- ASPerformBlockOnMainThread ( ^{
269+ [_mainSerialQueue performBlockOnMainThread: ^{
267270 [_completedNodes[kind] removeObjectsAtIndexes: indexSet];
268271 if (completionBlock) {
269272 completionBlock (indexSet);
270273 }
271- }) ;
274+ }] ;
272275}
273276
274277#pragma mark - Internal Data Querying + Editing
@@ -512,14 +515,14 @@ - (void)endUpdatesAnimated:(BOOL)animated completion:(void (^)(BOOL))completion
512515 LOG (@" endUpdatesWithCompletion - beginning" );
513516
514517 [_editingTransactionQueue addOperationWithBlock: ^{
515- ASPerformBlockOnMainThread ( ^{
518+ [_mainSerialQueue performBlockOnMainThread: ^{
516519 // Deep copy _completedNodes to _externalCompletedNodes.
517520 // Any external queries from now on will be done on _externalCompletedNodes, to guarantee data consistency with the delegate.
518521 _externalCompletedNodes = (NSMutableArray *)ASMultidimensionalArrayDeepMutableCopy (_completedNodes[ASDataControllerRowNodeKind]);
519522
520523 LOG (@" endUpdatesWithCompletion - begin updates call to delegate" );
521524 [_delegate dataControllerBeginUpdates: self ];
522- }) ;
525+ }] ;
523526 }];
524527
525528 // Running these commands may result in blocking on an _editingTransactionQueue operation that started even before -beginUpdates.
@@ -532,13 +535,13 @@ - (void)endUpdatesAnimated:(BOOL)animated completion:(void (^)(BOOL))completion
532535 [_pendingEditCommandBlocks removeAllObjects ];
533536
534537 [_editingTransactionQueue addOperationWithBlock: ^{
535- ASPerformBlockOnMainThread ( ^{
538+ [_mainSerialQueue performBlockOnMainThread: ^{
536539 // Now that the transaction is done, _completedNodes can be accessed externally again.
537540 _externalCompletedNodes = nil ;
538541
539542 LOG (@" endUpdatesWithCompletion - calling delegate end" );
540543 [_delegate dataController: self endUpdatesAnimated: animated completion: completion];
541- }) ;
544+ }] ;
542545 }];
543546 }
544547}
@@ -819,11 +822,11 @@ - (void)relayoutAllNodes
819822 // i.e there might be some nodes that were measured using the old constrained size but haven't been added to _completedNodes
820823 // (see _layoutNodes:atIndexPaths:withAnimationOptions:).
821824 [_editingTransactionQueue addOperationWithBlock: ^{
822- ASPerformBlockOnMainThread ( ^{
825+ [_mainSerialQueue performBlockOnMainThread: ^{
823826 for (NSString *kind in [_completedNodes keyEnumerator ]) {
824827 [self _relayoutNodesOfKind: kind];
825828 }
826- }) ;
829+ }] ;
827830 }];
828831 }];
829832}
0 commit comments