NSURLCache
서브 클래 싱하고 loadHTMLFromString:
을 호출 할 때마다 storeCachedRequest:forRequest:
을 호출 한 다음 cachedResponseForRequest:
을 호출합니다. 여기에 내가 가진 무엇 :NSURLCache 캐싱하지 않음
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
NSString *pathString = [[request URL] absoluteString];
NSCachedURLResponse * response = [super cachedResponseForRequest:request];
if (response != nil)
return response;
else {
NSString* myString= @"testing";
NSData* data=[myString dataUsingEncoding: NSASCIIStringEncoding ];
//
// Create the cacheable response
//
NSURLResponse *response =
[[[NSURLResponse alloc]
initWithURL:[request URL]
MIMEType:[self mimeTypeForPath:pathString]
expectedContentLength:[data length]
textEncodingName:nil]
autorelease];
NSCachedURLResponse * cachedResponse =
[[[NSCachedURLResponse alloc] initWithResponse:response data:data] autorelease];
[self storeCachedResponse:cachedResponse forRequest:request] ;
//NSLog(@"DEFAULT CACHE WITH URL %@", [[request URL] absoluteString]);
return [super cachedResponseForRequest:request];
}
return nil;
}
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request
{
[super storeCachedResponse:cachedResponse forRequest:request];
NSLog(@"STORE CACHED RESPONSE WITH URL %@", [[request URL] absoluteString]);
}
문제는 내가 저장 한 직후 cachedResponseForRequest:
를 호출 할 때, 응답이 항상 전무하다는 것이다. 왜 이런거야?
NSURLRequest * req = [NSURLRequest requestWithURL:[NSURL URLWithString:self.imageSource_]];
NSCachedURLResponse * response = [[NSURLCache sharedURLCache] cachedResponseForRequest:req];
if (response == nil)
NSLog(@"RESPONSE IS NIL");
else {
NSString* theString = [[NSString alloc] initWithData:response.data encoding:NSASCIIStringEncoding];
NSLog(@"RESPONSE IS NOT NIL %@", theString);
}
과 항상 응답이 전무하다 인쇄합니다. URL 문자열은 storeCachedResponse
일 때와 동일합니다. 어떤 이유로 캐싱하지 않습니다. 캐시 크기를 일정 크기로 설정했습니다.
흠 ... 그게 확실합니까? storeCachedResponse가 해당 메소드의 cachedResponseForRequest 및 response == nil 바로 전에 호출 되었기 때문에 – adit
자체 NSURLResponse를 만들지 않아도 여전히 저장하지 않습니다. – adit
"흠 ... 정말입니까?" 내 대답의 처음 세 단어를 다시 읽으십시오. ;-) –