Authored by Justin R. Miller

remove bottleneck of main queue for background cache callbacks

@@ -85,7 +85,9 @@ typedef enum : short { @@ -85,7 +85,9 @@ typedef enum : short {
85 85
86 #pragma mark - 86 #pragma mark -
87 87
88 -/** The RMTileCacheBackgroundDelegate protocol is for receiving notifications about background tile cache download operations. */ 88 +/** The RMTileCacheBackgroundDelegate protocol is for receiving notifications about background tile cache download operations.
  89 +*
  90 +* These callbacks are not guaranteed to be received on the main thread, so if you intend to do work in the user interface, you should properly enqueue such jobs on the main thread. */
89 @protocol RMTileCacheBackgroundDelegate <NSObject> 91 @protocol RMTileCacheBackgroundDelegate <NSObject>
90 92
91 @optional 93 @optional
@@ -377,21 +377,21 @@ @@ -377,21 +377,21 @@
377 377
378 [operation setCompletionBlock:^(void) 378 [operation setCompletionBlock:^(void)
379 { 379 {
380 - dispatch_sync(dispatch_get_main_queue(), ^(void) 380 + if ( ! [internalOperation isCancelled])
381 { 381 {
382 - if ( ! [internalOperation isCancelled])  
383 - {  
384 - progTile++; 382 + progTile++;
385 383
386 - if ([_backgroundCacheDelegate respondsToSelector:@selector(tileCache:didBackgroundCacheTile:withIndex:ofTotalTileCount:)])  
387 - {  
388 - [_backgroundCacheDelegate tileCache:weakSelf  
389 - didBackgroundCacheTile:RMTileMake((uint32_t)x, (uint32_t)y, zoom)  
390 - withIndex:progTile  
391 - ofTotalTileCount:totalTiles];  
392 - } 384 + if ([_backgroundCacheDelegate respondsToSelector:@selector(tileCache:didBackgroundCacheTile:withIndex:ofTotalTileCount:)])
  385 + {
  386 + [_backgroundCacheDelegate tileCache:weakSelf
  387 + didBackgroundCacheTile:RMTileMake((uint32_t)x, (uint32_t)y, zoom)
  388 + withIndex:progTile
  389 + ofTotalTileCount:totalTiles];
  390 + }
393 391
394 - if (progTile == totalTiles) 392 + if (progTile == totalTiles)
  393 + {
  394 + dispatch_async(dispatch_get_main_queue(), ^(void)
395 { 395 {
396 [weakSelf markCachingComplete]; 396 [weakSelf markCachingComplete];
397 397
@@ -399,9 +399,9 @@ @@ -399,9 +399,9 @@
399 { 399 {
400 [_backgroundCacheDelegate tileCacheDidFinishBackgroundCache:weakSelf]; 400 [_backgroundCacheDelegate tileCacheDidFinishBackgroundCache:weakSelf];
401 } 401 }
402 - } 402 + });
403 } 403 }
404 - }); 404 + }
405 }]; 405 }];
406 406
407 [_backgroundFetchQueue addOperation:operation]; 407 [_backgroundFetchQueue addOperation:operation];