开发者

clear cache and memory after using NSdata to download a PDF

开发者 https://www.devze.com 2023-03-17 04:00 出处:网络
How would I go about cleaning the cache data and other memory while using this code? CFData (store) on simulator keeps on growing....

How would I go about cleaning the cache data and other memory while using this code? CFData (store) on simulator keeps on growing....

-(void)downloadFile:(NSURL *)theURL
{
    NSLog(@"dowbload this url : %@",theURL);

    NSURL *url = theURL;

    NSData *data = [NSData dataWithContentsOfURL:url];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"PDF2.pdf"];

    [data writeToFile:pdfPath atomically:YES];

    [self showThePDF];
    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];
    [sh开发者_如何学GoaredCache removeAllCachedResponses];
    [sharedCache release];
}


I've had a similar problem to this recently.

Essentially dataWithContentsOfURL is using NSURLConnection under the hood, which caches the response.

I would recommend a couple of things:

Use NSURLConnection yourself to get the data instead of dataWithContentsOfURL.

Use the async NSURLConnection API and delegate methods (there's rarely a need for sync methods).

Implement the NSURLConnection delegate method below and return nil in it:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    return nil;
}

This ensures the responses are not cached.

NSURLConnection documentation: http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html

Using NSURLConnection: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html


Use this code.

I have the same problem with dataWithContentsOfURL. Try to add @autorealse:

            @autoreleasepool {
            NSError *error = nil;
            NSData *data=[[NSData alloc] init];
            data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
            img = [UIImage imageWithData:data];
            }


You need to release the variables: data, paths and pdfPath. The following calls:

   [NSData dataWithContentsOfURL:url]

   NSSearchPathForDirectoriesInDomains

   [documentsDirectory stringByAppendingPathComponent:@"DPR2.pdf"]

all return owned objects that you need to release in order to avoid memory leaks. You did not call init on them yourself but their internal implementations allocated memory on your behalf, then retained this memory (which is why you can use those vars without worrying about getting a segmentation fault). Even though your variables go out of scope when the method returns, the memory they allocated is being retained because you didn't decrement its reference count via release. I show roughly modified code before that should work:

-(void)downloadFile:(NSURL *)theURL 
{ 
    NSLog(@"dowbload this url : %@",theURL); 

    NSURL *url = theURL; 

    NSData *data = [NSData dataWithContentsOfURL:url]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"DPR2.pdf"]; 

    [data writeToFile:pdfPath atomically:YES]; 

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

    [pdfPath release];
    [paths release];
    [data release];
}

You can optionally choose to manage your dynamically allocated variables in an NSAutoReleasePool.

0

精彩评论

暂无评论...
验证码 换一张
取 消