iOS 8.0이 HTTP 응답 데이터를 캐시 할 수있는 NSURLSession
의 기능을 깨뜨린 것이 거의 확실합니다. 나는이 문제에 관해 애플과 레이더를 열었다.
여기에 내가 이것을 증명하기 위해 쓴 몇 가지 예제 코드입니다 :
NSURLSession *session = [NSURLSession sharedSession];
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
diskCapacity:32 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURL *URL = [NSURL URLWithString:@"http://i.imgur.com/b5pyONe.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:5];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"Error occurred");
} else {
NSLog(@"Fetched resource!");
}
dispatch_semaphore_signal(semaphore);
}];
[task resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
request = [NSURLRequest requestWithURL:URL
cachePolicy:NSURLRequestReturnCacheDataDontLoad
timeoutInterval:5];
task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"Something bad happened: %@", error);
} else {
NSLog(@"Fetched resource!");
}
}];
[task resume];
도 만들어 자신의
NSURLSession
-이 문제를 해결하지 않을 것이다 - 당신이 자신을 만들
NSURLCache
이있는
NSURLSessionConfiguration
와. 현재로서는 캐싱 응답이 잘못 필요할 경우
NSURLConnection
을 사용해야합니다.
NSURLSession과 매우 유사한 것을 발견했습니다. NSURLConnection NSURLCache 여전히 올바르게 작동하는 것 같아요. –
방금 사용자 지정 NSURLCache 하위 클래스 (storeCachedResponse : forRequest :가 호출되고 있지만 cachedResponseForRequest :가 호출되지 않음)와 동일한 (AFNetworking2 포함) 것으로 나타났습니다. –