Vì vậy, tôi đã phân loại NSURLCache
và mỗi lần tôi gọi loadHTMLFromString:
, hãy gọi storeCachedRequest:forRequest:
và sau đó cachedResponseForRequest:
. Dưới đây là những gì tôi có:NSURLCache không lưu vào bộ nhớ đệm
- (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]);
}
Vấn đề là khi tôi gọi cachedResponseForRequest:
ngay sau khi được lưu, phản hồi luôn là không. Tại sao điều này?
tôi đã:
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);
}
và nó luôn luôn in phản ứng là con số không. Chuỗi url giống với chuỗi storeCachedResponse
. Đối với một số lý do nó không phải là bộ nhớ đệm. Tôi đã đặt kích thước bộ nhớ cache thành một số kích thước.
hmmm ... bạn có chắc chắn về điều đó không? vì storeCachedResponse được gọi ngay trước cachedResponseForRequest và response == nil trong phương thức đó – adit
vì vậy ngay cả khi tôi không tạo NSURLResponse của riêng mình, nó vẫn không lưu nó – adit
"hmmm ... bạn có chắc về điều đó không?" Đọc lại ba từ đầu tiên của câu trả lời của tôi. ;-) –