Skip to content

Commit

Permalink
Fix delegate didFinish selector being called twice when using ASIRelo…
Browse files Browse the repository at this point in the history
…adIfDifferentCachePolicy

(Reported by Philippe Jayet)
  • Loading branch information
pokeb committed Aug 30, 2010
1 parent 6251bf1 commit 8998d7a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
11 changes: 6 additions & 5 deletions Classes/ASIHTTPRequest.m
Expand Up @@ -23,7 +23,7 @@


// Automatically set on build
NSString *ASIHTTPRequestVersion = @"v1.7-51 2010-08-18";
NSString *ASIHTTPRequestVersion = @"v1.7-52 2010-08-30";

NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";

Expand Down Expand Up @@ -652,6 +652,7 @@ - (void)main
}

[self setComplete:NO];
[self setDidUseCachedResponse:NO];

if (![self url]) {
[self failWithError:ASIUnableToCreateRequestError];
Expand Down Expand Up @@ -2829,7 +2830,7 @@ - (void)handleStreamComplete
}

// Save to the cache
if ([self downloadCache]) {
if ([self downloadCache] && ![self didUseCachedResponse]) {
[[self downloadCache] storeResponseForRequest:self maxAge:[self secondsToCache]];
}

Expand All @@ -2851,7 +2852,7 @@ - (void)handleStreamComplete
[self destroyReadStream];
}

if (![self needsRedirect] && ![self authenticationNeeded]) {
if (![self needsRedirect] && ![self authenticationNeeded] && ![self didUseCachedResponse]) {

if (fileError) {
[self failWithError:fileError];
Expand Down Expand Up @@ -2923,10 +2924,10 @@ - (BOOL)useDataFromCache
return NO;
}
}

// only 200 responses are stored in the cache, so let the client know
// this was a successful response
self.responseStatusCode = 200;
[self setResponseStatusCode:200];

[self setDidUseCachedResponse:YES];

Expand Down
2 changes: 1 addition & 1 deletion Classes/Tests/ASIDownloadCacheTests.h
Expand Up @@ -10,7 +10,7 @@


@interface ASIDownloadCacheTests : ASITestCase {

NSUInteger requestsFinishedCount;
}

@end
33 changes: 33 additions & 0 deletions Classes/Tests/ASIDownloadCacheTests.m
Expand Up @@ -291,4 +291,37 @@ - (void)testCookies
[ASIHTTPRequest setDefaultCache:nil];
}

// Text fix for a bug where the didFinishSelector would be called twice for a cached response using ASIReloadIfDifferentCachePolicy
- (void)testCacheOnlyCallsRequestFinishedOnce
{
// Run this request on the main thread to force delegate calls to happen synchronously
[self performSelectorOnMainThread:@selector(runCacheOnlyCallsRequestFinishedOnceTest) withObject:nil waitUntilDone:YES];
}

- (void)runCacheOnlyCallsRequestFinishedOnceTest
{
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]];
[request setCachePolicy:ASIReloadIfDifferentCachePolicy];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setDelegate:self];
[request startSynchronous];

requestsFinishedCount = 0;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]];
[request setCachePolicy:ASIReloadIfDifferentCachePolicy];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setDidFinishSelector:@selector(finishCached:)];
[request setDelegate:self];
[request startSynchronous];

BOOL success = (requestsFinishedCount == 1);
GHAssertTrue(success,@"didFinishSelector called more than once");
}

- (void)finishCached:(ASIHTTPRequest *)request
{
requestsFinishedCount++;
}

@end
1 change: 1 addition & 0 deletions Classes/Tests/ASINetworkQueueTests.m
Expand Up @@ -1201,6 +1201,7 @@ - (void)queueFailureFinish:(ASINetworkQueue *)queue
complete = YES;
}


@synthesize immediateCancelQueue;
@synthesize failedRequests;
@synthesize finishedRequests;
Expand Down

0 comments on commit 8998d7a

Please sign in to comment.