Skip to content

Instantly share code, notes, and snippets.

@rantav
Created June 6, 2011 23:06
Show Gist options
  • Save rantav/1011314 to your computer and use it in GitHub Desktop.
Save rantav/1011314 to your computer and use it in GitHub Desktop.
ASINetworkQueue - not calling my selectors.
#import <Foundation/Foundation.h>
@class ASINetworkQueue, Catalog, ASIHTTPRequest;
@interface DownloadCatalogOperation : NSOperation {
/* queue for downloading many assets in paralel */
ASINetworkQueue* networkQueue;
NSString* tmpDownloadPath;
Catalog* catalog;
int version;
}
- (void) requestFinished:(ASIHTTPRequest *)request;
- (void) requestFailed:(ASIHTTPRequest *)request;
- (void) queueFinished:(ASINetworkQueue *)queue;
@end
#import "DownloadCatalogOperation.h"
#import "Files.h"
#import "ApplicationPaths.h"
#import "Catalog.h"
#import "CatalogEntry.h"
#import "Server.h"
#import "ASINetworkQueue.h"
#import "ASIHTTPRequest.h"
#import "DDLog.h"
const static int ddLogLevel = LOG_LEVEL_VERBOSE;
#define TMP_CATALOG @"tmpCatalog"
#define MAX_CONCURRENT_DOWNLOADS 5
@implementation DownloadCatalogOperation
//
// Removed some parts of the class that aren't relevant to ASINetworkQueue
//
- (void) download:(NSString*)filename {
ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:serverUrlOfCatalogResource(filename)];
[request setDownloadDestinationPath:[tmpDownloadPath stringByAppendingPathComponent:filename]];
[networkQueue addOperation:request];
}
- (void) requestFinished:(ASIHTTPRequest *)request {
// This method doesn't get called
DDLogVerbose(@"Request finished, %d more to go... %@", [networkQueue requestsCount], request);
}
- (void) requestFailed:(ASIHTTPRequest *)request {
// This method doesn't get called
DDLogError(@"Failed to download one of the URLs, there were %d files remaining. %@", [networkQueue requestsCount], [request error]);
}
- (void) queueFinished:(ASINetworkQueue *)queue {
// This method doesn't get called
DDLogVerbose(@"All assets fetched successfully at %@", queue);
}
- (void) main {
// First download all to tmp
[networkQueue reset];
// Create a tmp path
tmpDownloadPath = [self createTmp];
for (NSString* resource in [catalog collectAllResourceNames]) {
[self download:resource];
}
[networkQueue go];
[networkQueue waitUntilAllOperationsAreFinished];
}
- (id)init {
self = [super init];
if (self) {
networkQueue = [[ASINetworkQueue queue] retain];
[networkQueue setMaxConcurrentOperationCount:MAX_CONCURRENT_DOWNLOADS];
[networkQueue setDelegate:self];
[networkQueue setRequestDidFinishSelector:@selector(requestFinished:)];
[networkQueue setRequestDidFailSelector:@selector(requestFailed:)];
[networkQueue setQueueDidFinishSelector:@selector(queueFinished:)];
}
return self;
}
- (void)dealloc {
[networkQueue release];
[catalog release];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment