У меня проблемы с кэшированием ответов NSURLConnection с использованием синхронного вызова. Я инициализирую кеш в одном классе, а затем использую его в другом. Обратите внимание, как объем кэш-памяти инициализируется до 100 КБ, но затем магически сбрасывается в ноль позже.
- (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