2010-01-28 4 views
4

동기 호출을 사용하여 NSURLConnection 응답을 캐싱하는 데 문제가 있습니다. 한 클래스에서 캐시를 초기화 한 다음 다른 클래스에서 캐시를 사용합니다. 캐시 메모리 용량이 100KB로 초기화되지만 나중에 마술처럼 0으로 재설정되는 것에 주목하십시오. 다른 클래스에서 NSURLCache 메모리 크기가 0입니다.

- (id)init { 
    if (self = [super init]) { 
     // Creates a custom URL cache that uses both memory and disk. 
     NSURLCache *sharedCache = 
     [[NSURLCache alloc] initWithMemoryCapacity:kMemoryCacheSize * 1000000 
     diskCapacity:kDiskCacheSize * 1000000 
     diskPath:diskPath]; 
     [NSURLCache setSharedURLCache:sharedCache]; 

     NSLog(@"Cache memory capacity = %d bytes", [[NSURLCache sharedURLCache] memoryCapacity]); 
     NSLog(@"Cache disk capacity = %d bytes", [[NSURLCache sharedURLCache] diskCapacity]); 

     //[sharedCache release]; 
    } 
    return self; 
} 
// NSLOG OUTPUT: 
// [6842:20b] Cache memory capacity = 100000 bytes 
// [6842:20b] Cache disk capacity = 20000000 bytes 

...

NSLog(@"Cache memory capacity = %d bytes", [[NSURLCache sharedURLCache] memoryCapacity]); 
NSLog(@"Cache disk capacity = %d bytes", [[NSURLCache sharedURLCache] diskCapacity]); 
NSLog(@"Cache Memory Usage = %d bytes", [[NSURLCache sharedURLCache] currentMemoryUsage]); 
NSLog(@"Cache Disc Usage = %d bytes", [[NSURLCache sharedURLCache] currentDiskUsage]); 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:objectURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:20]; 
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
NSLog(@"Image Request finished. Error = %@",[error localizedDescription]); 
NSLog(@"Cache size after = %d bytes", [[NSURLCache sharedURLCache] currentMemoryUsage]); 
// NSLog Output: 
// Cache memory capacity = 0 bytes 
// Cache Disc Usage = 0 bytes 
// Cache Memory Usage = 0 bytes 
// Cache disk capacity = 20000000 bytes 
// Image Request finished. Error = (null) 
// Cache size after = 0 bytes 

답변

1

나는 공유 객체가 동일하게 유지 있는지 확인하기 위해 NSLog 문에 "% 포인트"를 선택하고 [NSURLCache sharedURLCache]를 추가합니다. 그런 다음 중단 점을 + [NSURLCache setSharedURLCache :] 및 - [NSURLCache setMemoryCapacity :]에 추가하여 진행 상황을 확인합니다.

4

웹 뷰가있는 앱에서이 문제가 발생했습니다. 웹 뷰가 초기화 될 때 어떤 이유로 캐시가 제로인지, 왜인지는 알 수 없습니다. 이런 응용 프로그램에서는 setMemoryCapacity :가 0 인 경우 NSURLCache 하위 클래스를 사용합니다. 같은

뭔가 :

-(void)setMemoryCapacity:(NSUInteger)memCap 
{ 
    if (memCap == 0) 
     return; 
    [super setMemoryCapacity:memCap]; 
} 
0

여기에 같은 문제가 있었다. 생각 캐싱이 전혀 작동하지 않습니다. UIWebView없이 작은 테스트 프로젝트를 설정하면 [NSURLCache sharedURLCache] memoryCapacity]가 0 대신 예상 10000000을 반환합니다.

다행히도 캐시는 지워지지 않고 이전에 캐시 된 모든 개체가 남아 있습니다. Dougs 솔루션은 매력처럼 작동합니다!

BTW : 웹보기가 표시되면 크기가 0으로 설정됩니다. 나중에 10000000으로 다시 설정하면 웹보기가 표시되어 크기가 다시 0으로 설정되지 않습니다.

관련 문제