Ben Copsey

Change behaviour where ASINetworkQueues would take over as the delegate of all r…

…equests added to them
Requests added to queues now preserve their delegate settings
* Added 'queue' property to ASIHTTPRequest, ASINetworkQueues will set this, and requests will use this to notify queues when key things happen
* Update Mac download queue examplee to demonstrate this feature
* Fix a case sensitivity issue in one of the test URLs that would cause it to fail
* Remove debug logging in tests
... ... @@ -37,6 +37,9 @@ typedef enum _ASINetworkErrorType {
// The delegate, you need to manage setting and talking to your delegate in your subclasses
id delegate;
// A queue delegate that should *ALSO* be notified of delegate message
id queue;
// HTTP method to use (GET / POST / PUT / DELETE). Defaults to GET
NSString *requestMethod;
... ... @@ -349,6 +352,7 @@ typedef enum _ASINetworkErrorType {
@property (retain,readonly) NSURL *url;
@property (assign) id delegate;
@property (assign) id queue;
@property (assign) id uploadProgressDelegate;
@property (assign) id downloadProgressDelegate;
@property (assign) BOOL useKeychainPersistance;
... ...
This diff is collapsed. Click to expand it.
... ... @@ -9,7 +9,7 @@
@interface ASINetworkQueue : NSOperationQueue {
// Delegate will get didFail + didFinish messages (if set), as well as authorizationNeededForRequest messages
// Delegate will get didFail + didFinish messages (if set)
id delegate;
// Will be called when a request completes with the request as the argument
... ...
... ... @@ -16,7 +16,7 @@
{
self = [super init];
delegate = NULL;
delegate = nil;
requestDidFinishSelector = NULL;
requestDidFailSelector = NULL;
queueDidFinishSelector = NULL;
... ... @@ -113,17 +113,7 @@
[request setRequestMethod:@"HEAD"];
[request setQueuePriority:10];
[request setShowAccurateProgress:YES];
if (uploadProgressDelegate) {
[request setUploadProgressDelegate:self];
} else {
[request setUploadProgressDelegate:NULL];
}
if (downloadProgressDelegate) {
[request setDownloadProgressDelegate:self];
} else {
[request setDownloadProgressDelegate:NULL];
}
[request setDelegate:self];
[request setQueue:self];
[super addOperation:request];
}
}
... ... @@ -157,22 +147,7 @@
}
[request setShowAccurateProgress:showAccurateProgress];
if (uploadProgressDelegate) {
// For uploads requests, we always work out the total upload size before the queue starts, so we tell the request not to reset the progress indicator when starting each request
[request setShouldResetProgressIndicators:NO];
[request setUploadProgressDelegate:self];
} else {
[request setUploadProgressDelegate:NULL];
}
if (downloadProgressDelegate) {
[request setDownloadProgressDelegate:self];
} else {
[request setDownloadProgressDelegate:NULL];
}
[request setDelegate:self];
[request setDidFailSelector:@selector(requestDidFail:)];
[request setDidFinishSelector:@selector(requestDidFinish:)];
[request setQueue:self];
[super addOperation:request];
}
... ...
... ... @@ -36,8 +36,6 @@
[request setPostBodyFilePath:@"/Users/ben/Desktop/111.txt"];
[request start];
NSLog([request responseString]);
NSLog([NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"bigfile",size]);
BOOL success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"bigfile",size]]);
GHAssertTrue(success,@"Failed to upload the correct data (using local file)");
... ... @@ -49,8 +47,6 @@
[request setData:data forKey:@"file"];
[request start];
NSLog([request responseString]);
NSLog([NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"bigfile",size]);
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"file",size]]);
GHAssertTrue(success,@"Failed to upload the correct data (using NSData)");
}
... ...
... ... @@ -516,7 +516,6 @@
// [request start];
// err = [request error];
// GHAssertNil(err,@"Got an error when correct credentials were supplied");
// NSLog([request responseString]);
//}
- (void)testCompressedResponse
... ... @@ -551,7 +550,7 @@
[partialContent writeToFile:tempPath atomically:NO encoding:NSASCIIStringEncoding error:nil];
progress = 0;
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/test_partial_download.txt"] autorelease];
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/test_partial_download.txt"] autorelease];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setDownloadDestinationPath:downloadPath];
[request setTemporaryFileDownloadPath:tempPath];
... ...
... ... @@ -29,6 +29,9 @@
IBOutlet NSImageView *imageView1;
IBOutlet NSImageView *imageView2;
IBOutlet NSImageView *imageView3;
IBOutlet NSProgressIndicator *imageProgress1;
IBOutlet NSProgressIndicator *imageProgress2;
IBOutlet NSProgressIndicator *imageProgress3;
IBOutlet NSButton *startButton;
IBOutlet NSButton *resumeButton;
... ...
... ... @@ -101,22 +101,30 @@
[networkQueue cancelAllOperations];
[networkQueue setDownloadProgressDelegate:progressIndicator];
[networkQueue setRequestDidFinishSelector:@selector(imageFetchComplete:)];
[networkQueue setDelegate:self];
[networkQueue setShowAccurateProgress:([showAccurateProgress state] == NSOnState)];
ASIHTTPRequest *request;
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]] autorelease];
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/small-image.jpg"]] autorelease];
[request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"1.png"]];
[request setDownloadProgressDelegate:imageProgress1];
[request setDidFinishSelector:@selector(imageFetch1Complete:)];
[request setDelegate:self];
[networkQueue addOperation:request];
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/trailsnetwork.png"]] autorelease];
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/medium-image.jpg"]] autorelease];
[request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"2.png"]];
[request setDownloadProgressDelegate:imageProgress2];
[request setDidFinishSelector:@selector(imageFetch2Complete:)];
[request setDelegate:self];
[networkQueue addOperation:request];
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/sharedspace20.png"]] autorelease];
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/large-image.jpg"]] autorelease];
[request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"3.png"]];
[request setDownloadProgressDelegate:imageProgress3];
[request setDidFinishSelector:@selector(imageFetch3Complete:)];
[request setDelegate:self];
[networkQueue addOperation:request];
... ... @@ -124,24 +132,33 @@
}
- (void)imageFetchComplete:(ASIHTTPRequest *)request
- (void)imageFetch1Complete:(ASIHTTPRequest *)request
{
NSImage *img = [[[NSImage alloc] initWithContentsOfFile:[request downloadDestinationPath]] autorelease];
if (img) {
if ([imageView1 image]) {
if ([imageView2 image]) {
[imageView3 setImage:img];
} else {
[imageView2 setImage:img];
}
} else {
[imageView1 setImage:img];
}
[imageView1 setImage:img];
}
}
- (void)imageFetch2Complete:(ASIHTTPRequest *)request
{
NSImage *img = [[[NSImage alloc] initWithContentsOfFile:[request downloadDestinationPath]] autorelease];
if (img) {
[imageView2 setImage:img];
}
}
- (void)imageFetch3Complete:(ASIHTTPRequest *)request
{
NSImage *img = [[[NSImage alloc] initWithContentsOfFile:[request downloadDestinationPath]] autorelease];
if (img) {
[imageView3 setImage:img];
}
}
- (IBAction)fetchTopSecretInformation:(id)sender
{
[networkQueue cancelAllOperations];
... ...
This diff is collapsed. Click to expand it.