I think this should be a simple question. I've been asking a lot of these recently, but my app is almost done. Very exciting for me!
Here's the problem:
I have a ASINetworkQueue that is downloading a couple thousand map tiles in one shot from my S3 bucket. I also have a button in the main view that should cancel the queue. It works mostly, but when I tap the cancel button, it freezes the app for several seconds (up to a minute if I cancel early) while the queue calls the didFailSelector for each individual map tile that was in the queue.
Here's my code for the downloader (abbreviated where variables are declared and set):
Code:
-(void)downloadListOfItems {
[[self queue] cancelAllOperations];
[self setQueue:[ASINetworkQueue queue]];
for (int i = 1; i <= numberOfBatches; i++) {
ASIS3BucketRequest *listRequest = [ASIS3BucketRequest requestWithBucket:bucket];
[listRequest setAccessKey:accessKey];
[listRequest setSecretAccessKey:secretAccessKey];
NSString *pathToBatch = [NSString stringWithFormat:@"%@/batch%i", baseDirectory, i];
[listRequest setPrefix:pathToBatch];
[listRequest setDelegate:self];
[listRequest setDidFinishSelector:@selector(finishedDownloadingTileList:)];
[listRequest setDidFailSelector:@selector(failedDownloadingTileList:)];
[[self queue] addOperation:listRequest];
}
[self.queue go];
}
-(void)finishedDownloadingTileList:(ASIS3BucketRequest *)listRequest {
for (ASIS3BucketObject *object in[listRequest objects]) {
NSString *itemKey = [object key];
//Split key so that we can get the item's individual name
NSArray *array = [itemKey componentsSeparatedByString:@"/"];
NSString *folderName = [array objectAtIndex:1];
NSString *itemName = [array objectAtIndex:2];
[self downloadOneMapTile:itemName inFolder:folderName];
}
}
-(void)downloadOneMapTile: (NSString *)itemName inFolder:(NSString *)folderName {
//... set path to file in bucket and path for downloaded file
if (!fileAlreadyExists) {
ASIS3ObjectRequest *request = [ASIS3ObjectRequest requestWithBucket:bucket key:pathToFileInBucket];
[request setSecretAccessKey:secretAccessKey];
[request setAccessKey:accessKey];
[request setDownloadDestinationPath:pathForDownloadedFile];
[request setDelegate:self];
[request setDidFinishSelector:@selector(tileRequestDone:)];
[request setDidFailSelector:@selector(tileRequestFailed:)];
[self.queue addOperation:request];
}
}
And the cancel button code is quite simple:
Code:
[bulkDownloader.queue cancelAllOperations];
[bulkDownloader.queue setSuspended:YES];
[bulkDownloader.queue reset];
Thanks for any input.