0

Im using NSData -initWithContentsOfURL in a background thread to download some 4k images. I can see in Instruments that my CFData (store) keeps growing and goes upto 200MB (at which point btw I crash) although Im using this code to clear the cache

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache removeAllCachedResponses];
[sharedCache release];

that i found in this question

The part of the code that I know for sure is causing this problem (I commented it and the memory didn't grow beyond 50MB) is:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

NSError *error = nil;
NSString *serverPath = [serverImageInfo valueForKey:@"ImagePath"];
NSData *image = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[serverPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] options:NSDataReadingUncached error:&error];

NSString *directoryPath = [Utilities directorypath];
if (image != NULL && [image length] > 0)
{
    //NSString *path = [[NSString alloc] initWithString:directoryPath];

    NSString *path = [directoryPath stringByAppendingPathComponent:@"ArrangementImages"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:path])
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }

    path = [path stringByAppendingPathComponent:imageInfo.Name];
    [image writeToFile:path atomically:YES];
    self.imageInfo.ImagePath = path;
    [path release];
}

[sharedCache removeAllCachedResponses];
[sharedCache release];

//image = nil;
[image release];
Community
  • 1
  • 1
Bushra Shahid
  • 3,579
  • 1
  • 27
  • 37
  • So why not just remove that part? I'm not entirely convinced the url cache has anything to do with your code anyway (it is used with NSURLRequest objects, not NSURL objects) – borrrden May 10 '13 at 05:21
  • how do i download images if i remove that part??? – Bushra Shahid May 10 '13 at 05:32
  • @borrrden i replaced initWithContentsOfURL with NSURLRequest -sendSynchronousRequest.....no change in memory....allocations still show all my images as living objects in cfdata (store) and keeps growing as images are downloaded – Bushra Shahid May 10 '13 at 05:46
  • I don't think I understand your question. Which part are you commenting out? – borrrden May 10 '13 at 06:22
  • Did you check for leaks in instruments? – Wain May 10 '13 at 06:36
  • @borrrden i comment out the nsdata part and then everything is fine but i did it just to check where the problem was....i need hat to download images – Bushra Shahid May 10 '13 at 09:21
  • @xs2bush If you are going to be downloading files, I recommend using AFNetworking. It is easy to use and works very well. I don't see anything wrong with this code from looking at it a bit so I think the problem may be somewhere else (Do you use that data again somewhere else?) – borrrden May 10 '13 at 10:35
  • no. i actually found the solution i put this code in an autorelease pool and voila! – Bushra Shahid May 10 '13 at 10:54

1 Answers1

3

I just used [sharedCache removeAllCachedResponses]; in conjunction with an autorelease pool....and my cfdata(store) didnt increase beyond 15 MB although i downloaded 4k images.

Bushra Shahid
  • 3,579
  • 1
  • 27
  • 37