3

This does not occur in Xcode 4.4, but with 4.5, using ios6 simulator or real device, the call to sendSynchronousRequest does not return until timeout if called from within cachedResponseForRequest.

Likewise, a loop that uses sendAsynchronousRequest and spins (for 10 seconds checking every 0.05 seconds for the completion), finishes the loop (10 seconds passed), but never completes the request (if called from within cachedResponseForRequest).

In Xcode 4.4 (emulating ios5), this does not occur. Any ideas?

1 Answers1

0

just use the method "sendAsynchronousRequest". here is my solution and it works like a charm.

[NSURLConnection sendAsynchronousRequest:newRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data,NSError *error){
        NSLog(@"--- caching ---");
        if (error) {
            NSLog(@"%@", error);
            NSLog(@"not cached: %@", absoluteString);
        }
        NSString *filename = [self sha1:absoluteString];
        NSString *path = [cacheDirectory stringByAppendingPathComponent:filename];
        NSFileManager *fileManager = [[NSFileManager alloc] init];
        [fileManager createFileAtPath:path contents:data attributes:nil];
        [fileManager release];

        NSURLResponse *newResponse = [[NSURLResponse alloc] initWithURL:response.URL MIMEType:response.MIMEType expectedContentLength:data.length textEncodingName:nil];

        NSDictionary *responseInfo = [NSDictionary dictionaryWithObjectsAndKeys:filename, @"filename", newResponse.MIMEType, @"MIMEType", nil];
        [responsesInfo setObject:responseInfo forKey:absoluteString];
        cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:newResponse data:data];
        [newResponse release];
        [cachedResponses setObject:cachedResponse forKey:absoluteString];
        [cachedResponse release];
    }];

because it's only available in iOS5.0 and later. you may need to check the ios version first.

Jaden Gu
  • 9,143
  • 3
  • 23
  • 26