Authored by Thomas Rasch

o use NSURLConnection for network tile fetches

o retryCount and waitSeconds as properties
(Closes #23 and #24)
... ... @@ -28,11 +28,14 @@
#import "RMAbstractMercatorTileSource.h"
#import "RMProjection.h"
#define RMAbstractWebMapSourceRetryCount 3
#define RMAbstractWebMapSourceWaitSeconds 2
#define RMAbstractWebMapSourceDefaultRetryCount 3
#define RMAbstractWebMapSourceDefaultWaitSeconds 15.0
@interface RMAbstractWebMapSource : RMAbstractMercatorTileSource
@property (nonatomic, assign) NSUInteger retryCount;
@property (nonatomic, assign) NSTimeInterval waitSeconds;
- (NSURL *)URLForTile:(RMTile)tile;
// Tilesources with layers use this method.
... ...
... ... @@ -31,11 +31,16 @@
@implementation RMAbstractWebMapSource
@synthesize retryCount, waitSeconds;
- (id)init
{
if (!(self = [super init]))
return nil;
self.retryCount = RMAbstractWebMapSourceDefaultRetryCount;
self.waitSeconds = RMAbstractWebMapSourceDefaultWaitSeconds;
return self;
}
... ... @@ -74,12 +79,12 @@
//
NSMutableArray *tilesData = [NSMutableArray arrayWithCapacity:[URLs count]];
for (int p = 0; p < [URLs count]; p++)
for (int p = 0; p < [URLs count]; ++p)
[tilesData addObject:[NSNull null]];
dispatch_group_t fetchGroup = dispatch_group_create();
for (int u = 0; u < [URLs count]; u++)
for (int u = 0; u < [URLs count]; ++u)
{
NSURL *currentURL = [URLs objectAtIndex:u];
... ... @@ -87,11 +92,11 @@
{
NSData *tileData = nil;
for (int try = 0; try < RMAbstractWebMapSourceRetryCount; try++)
for (int try = 0; tileData == nil && try < self.retryCount; ++try)
{
if ( ! tileData)
// Beware: dataWithContentsOfURL is leaking like hell. Better use AFNetwork or ASIHTTPRequest
tileData = [NSData dataWithContentsOfURL:currentURL options:NSDataReadingUncached error:NULL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:currentURL];
[request setTimeoutInterval:(self.waitSeconds / (CGFloat)self.retryCount)];
tileData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}
if (tileData)
... ... @@ -108,7 +113,7 @@
// wait for whole group of fetches (with retries) to finish, then clean up
//
dispatch_group_wait(fetchGroup, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * RMAbstractWebMapSourceWaitSeconds));
dispatch_group_wait(fetchGroup, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * self.waitSeconds));
dispatch_release(fetchGroup);
// composite the collected images together
... ...
... ... @@ -463,7 +463,7 @@
}
averageX /= (double)enclosedAnnotationsCount;
averageY /= (double) enclosedAnnotationsCount;
averageY /= (double)enclosedAnnotationsCount;
double halfClusterMarkerWidth = clusterMarkerSize.width / 2.0,
halfClusterMarkerHeight = clusterMarkerSize.height / 2.0;
... ...