diff --git a/SDURLCache.m b/SDURLCache.m index a8bb5a5..2cb4df6 100644 --- a/SDURLCache.m +++ b/SDURLCache.m @@ -700,12 +700,23 @@ - (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request { dispatch_sync(get_disk_cache_queue(), ^{ NSMutableDictionary *accesses = [self.diskCacheInfo objectForKey:kAFURLCacheInfoAccessesKey]; if ([accesses objectForKey:cacheKey]) { // OPTI: Check for cache-hit in a in-memory dictionnary before to hit the FS - response = [NSKeyedUnarchiver unarchiveObjectWithFile:[_diskCachePath stringByAppendingPathComponent:cacheKey]]; - if (response) { - // OPTI: Log the entry last access time for LRU cache eviction algorithm but don't save the dictionary - // on disk now in order to save IO and time - [accesses setObject:[NSDate date] forKey:cacheKey]; - _diskCacheInfoDirty = YES; + @try { + response = [NSKeyedUnarchiver unarchiveObjectWithFile:[_diskCachePath stringByAppendingPathComponent:cacheKey]]; + if (response) { + // OPTI: Log the entry last access time for LRU cache eviction algorithm but don't save the dictionary + // on disk now in order to save IO and time + [accesses setObject:[NSDate date] forKey:cacheKey]; + _diskCacheInfoDirty = YES; + } + } + @catch (NSException *exception) { + if ([exception.name isEqualToString:NSInvalidArgumentException]) { + NSLog(@"Could not unarchive object at %@, Invalid archive!", [_diskCachePath stringByAppendingPathComponent:cacheKey]); + [self removeCachedResponseForRequest:request]; + } + } + @finally { + // do nothing } } });