2

I am using NSURLSession to download a large video file in background. In this iOS application I implemented pause, resume and stop downloading feature.

Here is my code :

in .h

@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask; 
@property (nonatomic, strong) NSURLSession *session;

in .m

NSURLSessionConfiguration *sessionConfiguration;
float ver = [[[UIDevice currentDevice] systemVersion] floatValue];

if(ver >= 8)
{
    sessionConfiguration =  [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"sessionID"];
}
else
{
    sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"sessionID"];
}

sessionConfiguration.HTTPMaximumConnectionsPerHost = 1;
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration
                                                 delegate:self
                                                 delegateQueue:nil];

For stop downloading I am using this method:

- (void)stopDownloading:(id)sender {        
    // Cancel the task.
    [self.fdi.downloadTask cancel];        
}

It works quite well with a large amount of files, but there is an inconvenience. When I stop downloading then Memory which is used in downloading is not released so the application size is continuously for stopped downloading tasks.

gevorg
  • 4,835
  • 4
  • 35
  • 52
ajeet sharma
  • 803
  • 10
  • 37

1 Answers1

0

Try using

[self.session invalidateAndCancel];

as Per documentation

It will cancels all outstanding tasks and then invalidates the session.

Also, You can fetch the cache directory and then by using a loop or something fetch and remove folder and data inside.

Rahul Verma
  • 688
  • 5
  • 17