title: ASIHTTPRequest - data from cache always empty
body: <p>I'm downloading images in my iPhone/iPad app - and because they remain the same most of the time, I'd like to cache them on disk:</p>
  
  <pre><code>ASIDownloadCache *cache = [ASIDownloadCache sharedCache];
  [asiRequest setDownloadCache:cache];
  [asiRequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
  [asiRequest setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
  [asiRequest setSecondsToCache:60*60*24*30]; // Cache for 30 days
  [asiRequest setDelegate:self]; // A delegate must be specified
  [asiRequest setDidFinishSelector:@selector(requestFinished:)];
  [asiRequest startAsynchronous];
  </code></pre>
  
  <p>In my requestFinished-method, it's recognized that the caching was successful, but the data inside my request-object is empyt; what am I missing?</p>
  
  <pre><code>- (void) requestFinished:(ASIHTTPRequest *)request {
     if ([request didUseCachedResponse]) {
         NSLog(@"cache, size: %i", [[request responseData] length]); // is always zero
         [self buildImageWithData:[request responseData]];
     }
     [...];
  }
  </code></pre>
  
  <p>Thanks a lot!</p>
  
  <p><strong>EDIT 1:</strong> my complete code which isn't working: the first time, I get the "not cached" statement, from then, "cache, size: 0"... I have NO idea why :(</p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
  
      for (int i = 0; i &lt; 10; i++) {
          asiRequest = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.myimageserver.com/myimage.jpg"]];
  
          ASIDownloadCache *cache = [ASIDownloadCache sharedCache];
          [asiRequest setDownloadCache:cache];
          [asiRequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
          [asiRequest setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
          [asiRequest setSecondsToCache:60*60*24*30]; // Cache for 30 days
          [asiRequest setDelegate:self]; // A delegate must be specified
          [asiRequest setDidFinishSelector:@selector(requestFinished:)];
          [asiRequest startAsynchronous];
      }
  }
  
  - (void) request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders {
      self.imageDataLoadedFromURL = [NSMutableData data];
  }
  
  - (void) request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data {
      [imageDataLoadedFromURL appendData:data];
  }
  
  - (void) requestFinished:(ASIHTTPRequest *)request {
      if ([request didUseCachedResponse]) {
          NSLog(@"cache, size: %i", [[request responseData] length]); // is always zero
      } else {
          NSLog(@"not cached");
      }
  }
  </code></pre>
  
  <p><strong>EDIT 2:</strong> seems like the cache already contains the 0-byte-image; so it's not the problem of reading from caching but writing to it...</p>
  
  <p><img src="https://i.stack.imgur.com/vh50o.png" alt="enter image description here"></p>
  
  <p><strong>EDIT 3:</strong> You can download the small test project from <a href="http://tinyurl.com/68wuwj2" rel="nofollow noreferrer">http://tinyurl.com/68wuwj2</a> </p>