CFData (store) increases upto 200MB when downloading images throughout NSData -initWithContentsOfURL in Background thread

StackOverflow https://stackoverflow.com/questions/16475590

Question

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];
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top