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 { @@ -37,6 +37,9 @@ typedef enum _ASINetworkErrorType {
37 // The delegate, you need to manage setting and talking to your delegate in your subclasses 37 // The delegate, you need to manage setting and talking to your delegate in your subclasses
38 id delegate; 38 id delegate;
39 39
  40 + // A queue delegate that should *ALSO* be notified of delegate message
  41 + id queue;
  42 +
40 // HTTP method to use (GET / POST / PUT / DELETE). Defaults to GET 43 // HTTP method to use (GET / POST / PUT / DELETE). Defaults to GET
41 NSString *requestMethod; 44 NSString *requestMethod;
42 45
@@ -349,6 +352,7 @@ typedef enum _ASINetworkErrorType { @@ -349,6 +352,7 @@ typedef enum _ASINetworkErrorType {
349 352
350 @property (retain,readonly) NSURL *url; 353 @property (retain,readonly) NSURL *url;
351 @property (assign) id delegate; 354 @property (assign) id delegate;
  355 +@property (assign) id queue;
352 @property (assign) id uploadProgressDelegate; 356 @property (assign) id uploadProgressDelegate;
353 @property (assign) id downloadProgressDelegate; 357 @property (assign) id downloadProgressDelegate;
354 @property (assign) BOOL useKeychainPersistance; 358 @property (assign) BOOL useKeychainPersistance;
This diff is collapsed. Click to expand it.
@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 9
10 @interface ASINetworkQueue : NSOperationQueue { 10 @interface ASINetworkQueue : NSOperationQueue {
11 11
12 - // Delegate will get didFail + didFinish messages (if set), as well as authorizationNeededForRequest messages 12 + // Delegate will get didFail + didFinish messages (if set)
13 id delegate; 13 id delegate;
14 14
15 // Will be called when a request completes with the request as the argument 15 // Will be called when a request completes with the request as the argument
@@ -16,7 +16,7 @@ @@ -16,7 +16,7 @@
16 { 16 {
17 self = [super init]; 17 self = [super init];
18 18
19 - delegate = NULL; 19 + delegate = nil;
20 requestDidFinishSelector = NULL; 20 requestDidFinishSelector = NULL;
21 requestDidFailSelector = NULL; 21 requestDidFailSelector = NULL;
22 queueDidFinishSelector = NULL; 22 queueDidFinishSelector = NULL;
@@ -113,17 +113,7 @@ @@ -113,17 +113,7 @@
113 [request setRequestMethod:@"HEAD"]; 113 [request setRequestMethod:@"HEAD"];
114 [request setQueuePriority:10]; 114 [request setQueuePriority:10];
115 [request setShowAccurateProgress:YES]; 115 [request setShowAccurateProgress:YES];
116 - if (uploadProgressDelegate) { 116 + [request setQueue:self];
117 - [request setUploadProgressDelegate:self];  
118 - } else {  
119 - [request setUploadProgressDelegate:NULL];  
120 - }  
121 - if (downloadProgressDelegate) {  
122 - [request setDownloadProgressDelegate:self];  
123 - } else {  
124 - [request setDownloadProgressDelegate:NULL];  
125 - }  
126 - [request setDelegate:self];  
127 [super addOperation:request]; 117 [super addOperation:request];
128 } 118 }
129 } 119 }
@@ -157,22 +147,7 @@ @@ -157,22 +147,7 @@
157 } 147 }
158 [request setShowAccurateProgress:showAccurateProgress]; 148 [request setShowAccurateProgress:showAccurateProgress];
159 149
160 - if (uploadProgressDelegate) { 150 + [request setQueue:self];
161 -  
162 - // 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  
163 - [request setShouldResetProgressIndicators:NO];  
164 - [request setUploadProgressDelegate:self];  
165 - } else {  
166 - [request setUploadProgressDelegate:NULL];  
167 - }  
168 - if (downloadProgressDelegate) {  
169 - [request setDownloadProgressDelegate:self];  
170 - } else {  
171 - [request setDownloadProgressDelegate:NULL];  
172 - }  
173 - [request setDelegate:self];  
174 - [request setDidFailSelector:@selector(requestDidFail:)];  
175 - [request setDidFinishSelector:@selector(requestDidFinish:)];  
176 [super addOperation:request]; 151 [super addOperation:request];
177 } 152 }
178 153
@@ -36,8 +36,6 @@ @@ -36,8 +36,6 @@
36 [request setPostBodyFilePath:@"/Users/ben/Desktop/111.txt"]; 36 [request setPostBodyFilePath:@"/Users/ben/Desktop/111.txt"];
37 [request start]; 37 [request start];
38 38
39 - NSLog([request responseString]);  
40 - NSLog([NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"bigfile",size]);  
41 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]]); 39 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]]);
42 GHAssertTrue(success,@"Failed to upload the correct data (using local file)"); 40 GHAssertTrue(success,@"Failed to upload the correct data (using local file)");
43 41
@@ -49,8 +47,6 @@ @@ -49,8 +47,6 @@
49 [request setData:data forKey:@"file"]; 47 [request setData:data forKey:@"file"];
50 [request start]; 48 [request start];
51 49
52 - NSLog([request responseString]);  
53 - NSLog([NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"bigfile",size]);  
54 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]]); 50 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]]);
55 GHAssertTrue(success,@"Failed to upload the correct data (using NSData)"); 51 GHAssertTrue(success,@"Failed to upload the correct data (using NSData)");
56 } 52 }
@@ -516,7 +516,6 @@ @@ -516,7 +516,6 @@
516 // [request start]; 516 // [request start];
517 // err = [request error]; 517 // err = [request error];
518 // GHAssertNil(err,@"Got an error when correct credentials were supplied"); 518 // GHAssertNil(err,@"Got an error when correct credentials were supplied");
519 -// NSLog([request responseString]);  
520 //} 519 //}
521 520
522 - (void)testCompressedResponse 521 - (void)testCompressedResponse
@@ -551,7 +550,7 @@ @@ -551,7 +550,7 @@
551 [partialContent writeToFile:tempPath atomically:NO encoding:NSASCIIStringEncoding error:nil]; 550 [partialContent writeToFile:tempPath atomically:NO encoding:NSASCIIStringEncoding error:nil];
552 551
553 progress = 0; 552 progress = 0;
554 - NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/test_partial_download.txt"] autorelease]; 553 + NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/test_partial_download.txt"] autorelease];
555 ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; 554 ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
556 [request setDownloadDestinationPath:downloadPath]; 555 [request setDownloadDestinationPath:downloadPath];
557 [request setTemporaryFileDownloadPath:tempPath]; 556 [request setTemporaryFileDownloadPath:tempPath];
@@ -29,6 +29,9 @@ @@ -29,6 +29,9 @@
29 IBOutlet NSImageView *imageView1; 29 IBOutlet NSImageView *imageView1;
30 IBOutlet NSImageView *imageView2; 30 IBOutlet NSImageView *imageView2;
31 IBOutlet NSImageView *imageView3; 31 IBOutlet NSImageView *imageView3;
  32 + IBOutlet NSProgressIndicator *imageProgress1;
  33 + IBOutlet NSProgressIndicator *imageProgress2;
  34 + IBOutlet NSProgressIndicator *imageProgress3;
32 35
33 IBOutlet NSButton *startButton; 36 IBOutlet NSButton *startButton;
34 IBOutlet NSButton *resumeButton; 37 IBOutlet NSButton *resumeButton;
@@ -101,22 +101,30 @@ @@ -101,22 +101,30 @@
101 101
102 [networkQueue cancelAllOperations]; 102 [networkQueue cancelAllOperations];
103 [networkQueue setDownloadProgressDelegate:progressIndicator]; 103 [networkQueue setDownloadProgressDelegate:progressIndicator];
104 - [networkQueue setRequestDidFinishSelector:@selector(imageFetchComplete:)];  
105 [networkQueue setDelegate:self]; 104 [networkQueue setDelegate:self];
106 [networkQueue setShowAccurateProgress:([showAccurateProgress state] == NSOnState)]; 105 [networkQueue setShowAccurateProgress:([showAccurateProgress state] == NSOnState)];
107 106
108 ASIHTTPRequest *request; 107 ASIHTTPRequest *request;
109 108
110 - request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]] autorelease]; 109 + request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/small-image.jpg"]] autorelease];
111 [request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"1.png"]]; 110 [request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"1.png"]];
  111 + [request setDownloadProgressDelegate:imageProgress1];
  112 + [request setDidFinishSelector:@selector(imageFetch1Complete:)];
  113 + [request setDelegate:self];
112 [networkQueue addOperation:request]; 114 [networkQueue addOperation:request];
113 115
114 - request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/trailsnetwork.png"]] autorelease]; 116 + request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/medium-image.jpg"]] autorelease];
115 [request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"2.png"]]; 117 [request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"2.png"]];
  118 + [request setDownloadProgressDelegate:imageProgress2];
  119 + [request setDidFinishSelector:@selector(imageFetch2Complete:)];
  120 + [request setDelegate:self];
116 [networkQueue addOperation:request]; 121 [networkQueue addOperation:request];
117 122
118 - request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/sharedspace20.png"]] autorelease]; 123 + request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/large-image.jpg"]] autorelease];
119 [request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"3.png"]]; 124 [request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"3.png"]];
  125 + [request setDownloadProgressDelegate:imageProgress3];
  126 + [request setDidFinishSelector:@selector(imageFetch3Complete:)];
  127 + [request setDelegate:self];
120 [networkQueue addOperation:request]; 128 [networkQueue addOperation:request];
121 129
122 130
@@ -124,24 +132,33 @@ @@ -124,24 +132,33 @@
124 } 132 }
125 133
126 134
127 -- (void)imageFetchComplete:(ASIHTTPRequest *)request 135 +- (void)imageFetch1Complete:(ASIHTTPRequest *)request
128 { 136 {
129 NSImage *img = [[[NSImage alloc] initWithContentsOfFile:[request downloadDestinationPath]] autorelease]; 137 NSImage *img = [[[NSImage alloc] initWithContentsOfFile:[request downloadDestinationPath]] autorelease];
130 if (img) { 138 if (img) {
131 - if ([imageView1 image]) {  
132 - if ([imageView2 image]) {  
133 - [imageView3 setImage:img];  
134 - } else {  
135 - [imageView2 setImage:img];  
136 - }  
137 - } else {  
138 [imageView1 setImage:img]; 139 [imageView1 setImage:img];
139 } 140 }
  141 +}
  142 +
  143 +- (void)imageFetch2Complete:(ASIHTTPRequest *)request
  144 +{
  145 + NSImage *img = [[[NSImage alloc] initWithContentsOfFile:[request downloadDestinationPath]] autorelease];
  146 + if (img) {
  147 + [imageView2 setImage:img];
140 } 148 }
  149 +}
  150 +
141 151
  152 +- (void)imageFetch3Complete:(ASIHTTPRequest *)request
  153 +{
  154 + NSImage *img = [[[NSImage alloc] initWithContentsOfFile:[request downloadDestinationPath]] autorelease];
  155 + if (img) {
  156 + [imageView3 setImage:img];
  157 + }
142 } 158 }
143 159
144 160
  161 +
145 - (IBAction)fetchTopSecretInformation:(id)sender 162 - (IBAction)fetchTopSecretInformation:(id)sender
146 { 163 {
147 [networkQueue cancelAllOperations]; 164 [networkQueue cancelAllOperations];
This diff is collapsed. Click to expand it.