Changed resume progress behaviour so that the resumed part of a download is incl…
…uded in the progress update Removed sharedNetworkQueue - thinking about it, this is potentially dangerous
Showing
10 changed files
with
78 additions
and
37 deletions
| @@ -122,6 +122,9 @@ typedef enum _ASINetworkErrorType { | @@ -122,6 +122,9 @@ typedef enum _ASINetworkErrorType { | ||
| 122 | // Size of the response | 122 | // Size of the response |
| 123 | unsigned long long contentLength; | 123 | unsigned long long contentLength; |
| 124 | 124 | ||
| 125 | + // Size of the partially downloaded content | ||
| 126 | + unsigned long long partialDownloadSize; | ||
| 127 | + | ||
| 125 | // Size of the POST payload | 128 | // Size of the POST payload |
| 126 | unsigned long long postLength; | 129 | unsigned long long postLength; |
| 127 | 130 | ||
| @@ -335,6 +338,7 @@ typedef enum _ASINetworkErrorType { | @@ -335,6 +338,7 @@ typedef enum _ASINetworkErrorType { | ||
| 335 | @property (retain) NSString *requestMethod; | 338 | @property (retain) NSString *requestMethod; |
| 336 | @property (retain,setter=setPostBody:) NSData *postBody; | 339 | @property (retain,setter=setPostBody:) NSData *postBody; |
| 337 | @property (assign) unsigned long long contentLength; | 340 | @property (assign) unsigned long long contentLength; |
| 341 | +@property (assign) unsigned long long partialDownloadSize; | ||
| 338 | @property (assign) unsigned long long postLength; | 342 | @property (assign) unsigned long long postLength; |
| 339 | @property (assign) BOOL shouldResetProgressIndicators; | 343 | @property (assign) BOOL shouldResetProgressIndicators; |
| 340 | @property (retain) ASIHTTPRequest *mainRequest; | 344 | @property (retain) ASIHTTPRequest *mainRequest; |
| @@ -79,6 +79,8 @@ static NSError *ASIUnableToCreateRequestError; | @@ -79,6 +79,8 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 79 | [self setAllowCompressedResponse:YES]; | 79 | [self setAllowCompressedResponse:YES]; |
| 80 | [self setDefaultResponseEncoding:NSISOLatin1StringEncoding]; | 80 | [self setDefaultResponseEncoding:NSISOLatin1StringEncoding]; |
| 81 | [self setUploadBufferSize:0]; | 81 | [self setUploadBufferSize:0]; |
| 82 | + [self setContentLength:0]; | ||
| 83 | + [self setPartialDownloadSize:0]; | ||
| 82 | [self setResponseHeaders:nil]; | 84 | [self setResponseHeaders:nil]; |
| 83 | [self setTimeOutSeconds:10]; | 85 | [self setTimeOutSeconds:10]; |
| 84 | [self setAllowResumeForFileDownloads:NO]; | 86 | [self setAllowResumeForFileDownloads:NO]; |
| @@ -277,8 +279,8 @@ static NSError *ASIUnableToCreateRequestError; | @@ -277,8 +279,8 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 277 | 279 | ||
| 278 | // Should this request resume an existing download? | 280 | // Should this request resume an existing download? |
| 279 | if ([self allowResumeForFileDownloads] && [self downloadDestinationPath] && [self temporaryFileDownloadPath] && [[NSFileManager defaultManager] fileExistsAtPath:[self temporaryFileDownloadPath]]) { | 281 | if ([self allowResumeForFileDownloads] && [self downloadDestinationPath] && [self temporaryFileDownloadPath] && [[NSFileManager defaultManager] fileExistsAtPath:[self temporaryFileDownloadPath]]) { |
| 280 | - unsigned long long downloadedSoFar = [[[NSFileManager defaultManager] fileAttributesAtPath:[self temporaryFileDownloadPath] traverseLink:NO] fileSize]; | 282 | + [self setPartialDownloadSize:[[[NSFileManager defaultManager] fileAttributesAtPath:[self temporaryFileDownloadPath] traverseLink:NO] fileSize]]; |
| 281 | - [self addRequestHeader:@"Range" value:[NSString stringWithFormat:@"bytes=%llu-",downloadedSoFar]]; | 283 | + [self addRequestHeader:@"Range" value:[NSString stringWithFormat:@"bytes=%llu-",partialDownloadSize]]; |
| 282 | } | 284 | } |
| 283 | 285 | ||
| 284 | // Add custom headers | 286 | // Add custom headers |
| @@ -622,7 +624,7 @@ static NSError *ASIUnableToCreateRequestError; | @@ -622,7 +624,7 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 622 | // We won't update download progress until we've examined the headers, since we might need to authenticate | 624 | // We won't update download progress until we've examined the headers, since we might need to authenticate |
| 623 | if (responseHeaders) { | 625 | if (responseHeaders) { |
| 624 | 626 | ||
| 625 | - unsigned long long bytesReadSoFar = totalBytesRead; | 627 | + unsigned long long bytesReadSoFar = totalBytesRead+partialDownloadSize; |
| 626 | 628 | ||
| 627 | if (bytesReadSoFar > lastBytesRead) { | 629 | if (bytesReadSoFar > lastBytesRead) { |
| 628 | [self setLastActivityTime:[NSDate date]]; | 630 | [self setLastActivityTime:[NSDate date]]; |
| @@ -644,8 +646,6 @@ static NSError *ASIUnableToCreateRequestError; | @@ -644,8 +646,6 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 644 | updatedProgress = YES; | 646 | updatedProgress = YES; |
| 645 | } | 647 | } |
| 646 | 648 | ||
| 647 | - | ||
| 648 | - | ||
| 649 | SEL selector = @selector(incrementDownloadProgressBy:); | 649 | SEL selector = @selector(incrementDownloadProgressBy:); |
| 650 | NSMethodSignature *signature = [[downloadProgressDelegate class] instanceMethodSignatureForSelector:selector]; | 650 | NSMethodSignature *signature = [[downloadProgressDelegate class] instanceMethodSignatureForSelector:selector]; |
| 651 | NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; | 651 | NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; |
| @@ -658,7 +658,7 @@ static NSError *ASIUnableToCreateRequestError; | @@ -658,7 +658,7 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 658 | 658 | ||
| 659 | // We aren't using a queue, we should just set progress of the indicator to 0 | 659 | // We aren't using a queue, we should just set progress of the indicator to 0 |
| 660 | } else if (contentLength > 0) { | 660 | } else if (contentLength > 0) { |
| 661 | - [ASIHTTPRequest setProgress:(double)(1.0*bytesReadSoFar/contentLength) forProgressIndicator:downloadProgressDelegate]; | 661 | + [ASIHTTPRequest setProgress:(double)(1.0*bytesReadSoFar/(contentLength+partialDownloadSize)) forProgressIndicator:downloadProgressDelegate]; |
| 662 | } | 662 | } |
| 663 | } | 663 | } |
| 664 | 664 | ||
| @@ -789,7 +789,7 @@ static NSError *ASIUnableToCreateRequestError; | @@ -789,7 +789,7 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 789 | [mainRequest setContentLength:contentLength]; | 789 | [mainRequest setContentLength:contentLength]; |
| 790 | } | 790 | } |
| 791 | if (downloadProgressDelegate && showAccurateProgress && shouldResetProgressIndicators) { | 791 | if (downloadProgressDelegate && showAccurateProgress && shouldResetProgressIndicators) { |
| 792 | - [self resetDownloadProgress:contentLength]; | 792 | + [self resetDownloadProgress:contentLength+partialDownloadSize]; |
| 793 | } | 793 | } |
| 794 | } | 794 | } |
| 795 | 795 | ||
| @@ -1453,6 +1453,7 @@ static NSError *ASIUnableToCreateRequestError; | @@ -1453,6 +1453,7 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 1453 | @synthesize requestMethod; | 1453 | @synthesize requestMethod; |
| 1454 | @synthesize postBody; | 1454 | @synthesize postBody; |
| 1455 | @synthesize contentLength; | 1455 | @synthesize contentLength; |
| 1456 | +@synthesize partialDownloadSize; | ||
| 1456 | @synthesize postLength; | 1457 | @synthesize postLength; |
| 1457 | @synthesize shouldResetProgressIndicators; | 1458 | @synthesize shouldResetProgressIndicators; |
| 1458 | @synthesize mainRequest; | 1459 | @synthesize mainRequest; |
| @@ -83,8 +83,6 @@ | @@ -83,8 +83,6 @@ | ||
| 83 | // This method will start the queue | 83 | // This method will start the queue |
| 84 | - (void)go; | 84 | - (void)go; |
| 85 | 85 | ||
| 86 | -// A globally available network queue, created the first time it is requested | ||
| 87 | -+ (ASINetworkQueue *)sharedNetworkQueue; | ||
| 88 | 86 | ||
| 89 | @property (assign,setter=setUploadProgressDelegate:) id uploadProgressDelegate; | 87 | @property (assign,setter=setUploadProgressDelegate:) id uploadProgressDelegate; |
| 90 | @property (assign,setter=setDownloadProgressDelegate:) id downloadProgressDelegate; | 88 | @property (assign,setter=setDownloadProgressDelegate:) id downloadProgressDelegate; |
| @@ -140,13 +140,6 @@ static ASINetworkQueue *sharedNetworkQueue = nil; | @@ -140,13 +140,6 @@ static ASINetworkQueue *sharedNetworkQueue = nil; | ||
| 140 | if ([[request requestMethod] isEqualToString:@"GET"]) { | 140 | if ([[request requestMethod] isEqualToString:@"GET"]) { |
| 141 | ASIHTTPRequest *HEADRequest = [[[ASIHTTPRequest alloc] initWithURL:[request url]] autorelease]; | 141 | ASIHTTPRequest *HEADRequest = [[[ASIHTTPRequest alloc] initWithURL:[request url]] autorelease]; |
| 142 | [HEADRequest setMainRequest:request]; | 142 | [HEADRequest setMainRequest:request]; |
| 143 | - | ||
| 144 | - //If we're downloading to a file, and we already have a partial download to start from | ||
| 145 | - if ([request allowResumeForFileDownloads] && [request downloadDestinationPath] && [request temporaryFileDownloadPath] && [[NSFileManager defaultManager] fileExistsAtPath:[request temporaryFileDownloadPath]]) { | ||
| 146 | - unsigned long long downloadedSoFar = [[[NSFileManager defaultManager] fileAttributesAtPath:[request temporaryFileDownloadPath] traverseLink:NO] fileSize]; | ||
| 147 | - [HEADRequest addRequestHeader:@"Range" value:[NSString stringWithFormat:@"bytes=%llu-",downloadedSoFar]]; | ||
| 148 | - } | ||
| 149 | - | ||
| 150 | [self addHEADOperation:HEADRequest]; | 143 | [self addHEADOperation:HEADRequest]; |
| 151 | 144 | ||
| 152 | //Tell the request not to reset the progress indicator when it gets a content-length, as we will get the length from the HEAD request | 145 | //Tell the request not to reset the progress indicator when it gets a content-length, as we will get the length from the HEAD request |
| @@ -289,13 +282,6 @@ static ASINetworkQueue *sharedNetworkQueue = nil; | @@ -289,13 +282,6 @@ static ASINetworkQueue *sharedNetworkQueue = nil; | ||
| 289 | } | 282 | } |
| 290 | 283 | ||
| 291 | 284 | ||
| 292 | -+ (ASINetworkQueue *)sharedNetworkQueue | ||
| 293 | -{ | ||
| 294 | - if (!sharedNetworkQueue) { | ||
| 295 | - sharedNetworkQueue = [[ASINetworkQueue alloc] init]; | ||
| 296 | - } | ||
| 297 | - return sharedNetworkQueue; | ||
| 298 | -} | ||
| 299 | 285 | ||
| 300 | 286 | ||
| 301 | @synthesize uploadProgressDelegate; | 287 | @synthesize uploadProgressDelegate; |
| @@ -11,7 +11,6 @@ | @@ -11,7 +11,6 @@ | ||
| 11 | 11 | ||
| 12 | @implementation ASIFormDataRequestTests | 12 | @implementation ASIFormDataRequestTests |
| 13 | 13 | ||
| 14 | - | ||
| 15 | - (void)testPostWithFileUpload | 14 | - (void)testPostWithFileUpload |
| 16 | { | 15 | { |
| 17 | NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/post"]; | 16 | NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/post"]; |
| @@ -511,12 +511,15 @@ | @@ -511,12 +511,15 @@ | ||
| 511 | NSString *partialContent = @"This file should be exactly 163 bytes long when encoded as UTF8, Unix line breaks with no BOM.\n"; | 511 | NSString *partialContent = @"This file should be exactly 163 bytes long when encoded as UTF8, Unix line breaks with no BOM.\n"; |
| 512 | [partialContent writeToFile:tempPath atomically:NO encoding:NSASCIIStringEncoding error:nil]; | 512 | [partialContent writeToFile:tempPath atomically:NO encoding:NSASCIIStringEncoding error:nil]; |
| 513 | 513 | ||
| 514 | + progress = 0; | ||
| 514 | NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/test_partial_download.txt"] autorelease]; | 515 | NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/test_partial_download.txt"] autorelease]; |
| 515 | ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | 516 | ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
| 516 | [request setDownloadDestinationPath:downloadPath]; | 517 | [request setDownloadDestinationPath:downloadPath]; |
| 517 | [request setTemporaryFileDownloadPath:tempPath]; | 518 | [request setTemporaryFileDownloadPath:tempPath]; |
| 518 | [request setAllowResumeForFileDownloads:YES]; | 519 | [request setAllowResumeForFileDownloads:YES]; |
| 520 | + [request setDownloadProgressDelegate:self]; | ||
| 519 | [request start]; | 521 | [request start]; |
| 522 | + | ||
| 520 | 523 | ||
| 521 | BOOL success = ([request contentLength] == 68); | 524 | BOOL success = ([request contentLength] == 68); |
| 522 | GHAssertTrue(success,@"Failed to download a segment of the data"); | 525 | GHAssertTrue(success,@"Failed to download a segment of the data"); |
| @@ -527,6 +530,9 @@ | @@ -527,6 +530,9 @@ | ||
| 527 | success = ([newPartialContent isEqualToString:@"This is the content we ought to be getting if we start from byte 95."]); | 530 | success = ([newPartialContent isEqualToString:@"This is the content we ought to be getting if we start from byte 95."]); |
| 528 | GHAssertTrue(success,@"Failed to append the correct data to the end of the file?"); | 531 | GHAssertTrue(success,@"Failed to append the correct data to the end of the file?"); |
| 529 | 532 | ||
| 533 | + success = (progress == 1); | ||
| 534 | + GHAssertTrue(success,@"Failed to correctly display increment progress for a partial download"); | ||
| 530 | } | 535 | } |
| 531 | 536 | ||
| 537 | + | ||
| 532 | @end | 538 | @end |
| @@ -13,8 +13,6 @@ | @@ -13,8 +13,6 @@ | ||
| 13 | @implementation ASINetworkQueueTests | 13 | @implementation ASINetworkQueueTests |
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | -static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMode"); | ||
| 17 | - | ||
| 18 | - (void)testProgress | 16 | - (void)testProgress |
| 19 | { | 17 | { |
| 20 | complete = NO; | 18 | complete = NO; |
| @@ -40,10 +38,10 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | @@ -40,10 +38,10 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | ||
| 40 | [networkQueue addOperation:request3]; | 38 | [networkQueue addOperation:request3]; |
| 41 | 39 | ||
| 42 | [networkQueue go]; | 40 | [networkQueue go]; |
| 43 | - | 41 | + |
| 44 | - while (!complete) { | 42 | + while (!complete) { |
| 45 | - CFRunLoopRunInMode(ASIHTTPRequestTestsRunMode,0.25,YES); | 43 | + [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]]; |
| 46 | - } | 44 | + } |
| 47 | 45 | ||
| 48 | BOOL success = (progress == 1.0); | 46 | BOOL success = (progress == 1.0); |
| 49 | GHAssertTrue(success,@"Failed to increment progress properly"); | 47 | GHAssertTrue(success,@"Failed to increment progress properly"); |
| @@ -80,7 +78,7 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | @@ -80,7 +78,7 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | ||
| 80 | 78 | ||
| 81 | } | 79 | } |
| 82 | 80 | ||
| 83 | - | 81 | + |
| 84 | 82 | ||
| 85 | - (void)setProgress:(float)newProgress | 83 | - (void)setProgress:(float)newProgress |
| 86 | { | 84 | { |
| @@ -213,6 +211,7 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | @@ -213,6 +211,7 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | ||
| 213 | } | 211 | } |
| 214 | 212 | ||
| 215 | 213 | ||
| 214 | + | ||
| 216 | - (void)testProgressWithAuthentication | 215 | - (void)testProgressWithAuthentication |
| 217 | { | 216 | { |
| 218 | complete = NO; | 217 | complete = NO; |
| @@ -234,7 +233,7 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | @@ -234,7 +233,7 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | ||
| 234 | 233 | ||
| 235 | 234 | ||
| 236 | while (!complete) { | 235 | while (!complete) { |
| 237 | - CFRunLoopRunInMode(ASIHTTPRequestTestsRunMode,0.25,YES); | 236 | + [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]]; |
| 238 | } | 237 | } |
| 239 | 238 | ||
| 240 | NSError *error = [request error]; | 239 | NSError *error = [request error]; |
| @@ -256,7 +255,7 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | @@ -256,7 +255,7 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | ||
| 256 | [networkQueue go]; | 255 | [networkQueue go]; |
| 257 | 256 | ||
| 258 | while (!complete) { | 257 | while (!complete) { |
| 259 | - CFRunLoopRunInMode(ASIHTTPRequestTestsRunMode,0.25,YES); | 258 | + [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]]; |
| 260 | } | 259 | } |
| 261 | 260 | ||
| 262 | error = [request error]; | 261 | error = [request error]; |
| @@ -265,6 +264,8 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | @@ -265,6 +264,8 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | ||
| 265 | 264 | ||
| 266 | } | 265 | } |
| 267 | 266 | ||
| 267 | + | ||
| 268 | + | ||
| 268 | - (void)requestFailedExpectedly:(ASIHTTPRequest *)request | 269 | - (void)requestFailedExpectedly:(ASIHTTPRequest *)request |
| 269 | { | 270 | { |
| 270 | request_didfail = YES; | 271 | request_didfail = YES; |
| @@ -306,6 +307,7 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | @@ -306,6 +307,7 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | ||
| 306 | - (void)testPartialResume | 307 | - (void)testPartialResume |
| 307 | { | 308 | { |
| 308 | complete = NO; | 309 | complete = NO; |
| 310 | + progress = 0; | ||
| 309 | 311 | ||
| 310 | NSString *temporaryPath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.download"]; | 312 | NSString *temporaryPath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.download"]; |
| 311 | if ([[NSFileManager defaultManager] fileExistsAtPath:temporaryPath]) { | 313 | if ([[NSFileManager defaultManager] fileExistsAtPath:temporaryPath]) { |
| @@ -339,8 +341,13 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | @@ -339,8 +341,13 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | ||
| 339 | 341 | ||
| 340 | [networkQueue release]; | 342 | [networkQueue release]; |
| 341 | networkQueue = [[ASINetworkQueue alloc] init]; | 343 | networkQueue = [[ASINetworkQueue alloc] init]; |
| 344 | + [networkQueue setDownloadProgressDelegate:self]; | ||
| 345 | + [networkQueue setShowAccurateProgress:YES]; | ||
| 346 | + [networkQueue setDelegate:self]; | ||
| 347 | + [networkQueue setQueueDidFinishSelector:@selector(queueFinished:)]; | ||
| 342 | 348 | ||
| 343 | - | 349 | + complete = NO; |
| 350 | + progress = 0; | ||
| 344 | unsigned long long downloadedSoFar = [[[NSFileManager defaultManager] fileAttributesAtPath:temporaryPath traverseLink:NO] fileSize]; | 351 | unsigned long long downloadedSoFar = [[[NSFileManager defaultManager] fileAttributesAtPath:temporaryPath traverseLink:NO] fileSize]; |
| 345 | BOOL success = (downloadedSoFar > 0); | 352 | BOOL success = (downloadedSoFar > 0); |
| 346 | GHAssertTrue(success,@"Failed to download part of the file, so we can't proceed with this test"); | 353 | GHAssertTrue(success,@"Failed to download part of the file, so we can't proceed with this test"); |
| @@ -351,14 +358,19 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | @@ -351,14 +358,19 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | ||
| 351 | [request setAllowResumeForFileDownloads:YES]; | 358 | [request setAllowResumeForFileDownloads:YES]; |
| 352 | 359 | ||
| 353 | [networkQueue addOperation:request]; | 360 | [networkQueue addOperation:request]; |
| 361 | + | ||
| 354 | [networkQueue go]; | 362 | [networkQueue go]; |
| 355 | - | 363 | + |
| 356 | - [networkQueue waitUntilAllOperationsAreFinished]; | 364 | + while (!complete) { |
| 365 | + [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]]; | ||
| 366 | + } | ||
| 357 | 367 | ||
| 358 | unsigned long long amountDownloaded = [[[NSFileManager defaultManager] fileAttributesAtPath:downloadPath traverseLink:NO] fileSize]; | 368 | unsigned long long amountDownloaded = [[[NSFileManager defaultManager] fileAttributesAtPath:downloadPath traverseLink:NO] fileSize]; |
| 359 | success = (amountDownloaded == 9145357); | 369 | success = (amountDownloaded == 9145357); |
| 360 | GHAssertTrue(success,@"Failed to complete the download"); | 370 | GHAssertTrue(success,@"Failed to complete the download"); |
| 361 | 371 | ||
| 372 | + success = (progress == 1.0); | ||
| 373 | + GHAssertTrue(success,@"Failed to increment progress properly"); | ||
| 362 | 374 | ||
| 363 | [networkQueue release]; | 375 | [networkQueue release]; |
| 364 | 376 | ||
| @@ -372,4 +384,5 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | @@ -372,4 +384,5 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod | ||
| 372 | } | 384 | } |
| 373 | 385 | ||
| 374 | 386 | ||
| 387 | + | ||
| 375 | @end | 388 | @end |
| @@ -29,11 +29,15 @@ | @@ -29,11 +29,15 @@ | ||
| 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 | + | ||
| 33 | + IBOutlet NSButton *startButton; | ||
| 34 | + IBOutlet NSButton *resumeButton; | ||
| 32 | } | 35 | } |
| 33 | 36 | ||
| 34 | - (IBAction)simpleURLFetch:(id)sender; | 37 | - (IBAction)simpleURLFetch:(id)sender; |
| 35 | - (IBAction)URLFetchWithProgress:(id)sender; | 38 | - (IBAction)URLFetchWithProgress:(id)sender; |
| 36 | - | 39 | +- (IBAction)stopURLFetchWithProgress:(id)sender; |
| 40 | +- (IBAction)resumeURLFetchWithProgress:(id)sender; | ||
| 37 | 41 | ||
| 38 | - (IBAction)fetchThreeImages:(id)sender; | 42 | - (IBAction)fetchThreeImages:(id)sender; |
| 39 | 43 |
| @@ -42,6 +42,32 @@ | @@ -42,6 +42,32 @@ | ||
| 42 | 42 | ||
| 43 | - (IBAction)URLFetchWithProgress:(id)sender | 43 | - (IBAction)URLFetchWithProgress:(id)sender |
| 44 | { | 44 | { |
| 45 | + [startButton setTitle:@"Stop"]; | ||
| 46 | + [startButton setAction:@selector(stopURLFetchWithProgress:)]; | ||
| 47 | + | ||
| 48 | + NSString *tempFile = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.download"]; | ||
| 49 | + if ([[NSFileManager defaultManager] fileExistsAtPath:tempFile]) { | ||
| 50 | + [[NSFileManager defaultManager] removeItemAtPath:tempFile error:nil]; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + [self resumeURLFetchWithProgress:self]; | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | + | ||
| 57 | +- (IBAction)stopURLFetchWithProgress:(id)sender | ||
| 58 | +{ | ||
| 59 | + [startButton setTitle:@"Start"]; | ||
| 60 | + [startButton setAction:@selector(URLFetchWithProgress:)]; | ||
| 61 | + [networkQueue cancelAllOperations]; | ||
| 62 | + [resumeButton setEnabled:YES]; | ||
| 63 | +} | ||
| 64 | + | ||
| 65 | +- (IBAction)resumeURLFetchWithProgress:(id)sender | ||
| 66 | +{ | ||
| 67 | + [resumeButton setEnabled:NO]; | ||
| 68 | + [startButton setTitle:@"Stop"]; | ||
| 69 | + [startButton setAction:@selector(stopURLFetchWithProgress:)]; | ||
| 70 | + | ||
| 45 | [networkQueue cancelAllOperations]; | 71 | [networkQueue cancelAllOperations]; |
| 46 | [networkQueue setShowAccurateProgress:YES]; | 72 | [networkQueue setShowAccurateProgress:YES]; |
| 47 | [networkQueue setDownloadProgressDelegate:progressIndicator]; | 73 | [networkQueue setDownloadProgressDelegate:progressIndicator]; |
| @@ -50,6 +76,8 @@ | @@ -50,6 +76,8 @@ | ||
| 50 | 76 | ||
| 51 | ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://trails-network.net/Downloads/MemexTrails_1.0b1.zip"]] autorelease]; | 77 | ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://trails-network.net/Downloads/MemexTrails_1.0b1.zip"]] autorelease]; |
| 52 | [request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip"]]; | 78 | [request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip"]]; |
| 79 | + [request setTemporaryFileDownloadPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.download"]]; | ||
| 80 | + [request setAllowResumeForFileDownloads:YES]; | ||
| 53 | [networkQueue addOperation:request]; | 81 | [networkQueue addOperation:request]; |
| 54 | [networkQueue go]; | 82 | [networkQueue go]; |
| 55 | } | 83 | } |
| @@ -61,6 +89,8 @@ | @@ -61,6 +89,8 @@ | ||
| 61 | } else { | 89 | } else { |
| 62 | [fileLocation setStringValue:[NSString stringWithFormat:@"File downloaded to %@",[request downloadDestinationPath]]]; | 90 | [fileLocation setStringValue:[NSString stringWithFormat:@"File downloaded to %@",[request downloadDestinationPath]]]; |
| 63 | } | 91 | } |
| 92 | + [startButton setTitle:@"Start"]; | ||
| 93 | + [startButton setAction:@selector(URLFetchWithProgress:)]; | ||
| 64 | } | 94 | } |
| 65 | 95 | ||
| 66 | - (IBAction)fetchThreeImages:(id)sender | 96 | - (IBAction)fetchThreeImages:(id)sender |
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment