Trouble with NSURLCache

  • Hi,

    Can somebody explain whats's wrong here? I cache a NSData using
    [NSURLCache storeCachedResponse] but if I try to fetch it immediately
    (or after a while), the cachedResponseForRequest:request is always nil?

    I came on this problem while trying to understand why my app didnt
    cache the data on Leopard (but works OK on Tiger).
    I tried to create my own cache, but it still doesnt work.

    I use

      NSString *path = [NSString stringWithString:@"/Library/Desktop
    Pictures/Nature/Clown Fish.jpg"];
      NSImage*  diskImage;

      diskImage = [[NSImage alloc] initWithContentsOfFile:path];
      [[NSURLCache sharedURLCache] cacheData:[diskImage
    TIFFRepresentation] forPath:path];

    to cache the data, and I add this category to NSURLCache:
    The code comes from a post from Dan Wood.

    - (void) cacheData:(NSData *)aData forPath:(NSString *)aPath;
    {

      NSURL *cacheURL = [NSURL fileURLWithPath:aPath];
      NSURLResponse *response = [[[NSURLResponse alloc]
                                  initWithURL:cacheURL
                                  MIMEType:@"application/octet-stream"
                                  expectedContentLength:[aData length]
                                  textEncodingName:nil] autorelease];
      NSCachedURLResponse *cachedResponse = [[[NSCachedURLResponse alloc]
                                              initWithResponse:response
    data:aData userInfo:nil storagePolicy:NSURLCacheStorageAllowed]
    autorelease];
      NSURLRequest *request = [NSURLRequest requestWithURL:cacheURL
    cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:300];
      [self storeCachedResponse:cachedResponse forRequest:request];
      if ([self cachedResponseForRequest:request] == NULL) {
        NSLog(@"Something wrong here.");
      }
    }

    Thanks,
    Stephan
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today