Skip to content

Commit

Permalink
Added close session to example
Browse files Browse the repository at this point in the history
  • Loading branch information
Zen-CODE committed Apr 25, 2016
1 parent 6157496 commit b1e5ae8
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/background_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def download_file(self, url):
self.task = self.session.downloadTaskWithURL_(oc_url)
self.task.resume()

def close_session(self):
""" Close the session. This is required to prevent memory leaks after
all the downloads have completed.
https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/#//apple_ref/occ/instm/NSURLSession/downloadTaskWithURL:
"""
self.session.finishTasksAndInvalidate()

@protocol('NSURLSessionDownloadDelegate')
def URLSession_downloadTask_didWriteData_totalBytesWritten_totalBytesExpectedToWrite_(self, *args):
Logger.info(
Expand All @@ -79,6 +87,7 @@ def URLSession_downloadTask_didFinishDownloadingToURL_(self, *args):
Logger.info(
'Downloaded file is {0}.\nYou need to move this before the '
'function returns.'.format(ns_url.fileSystemRepresentation))
self.close_session()

@protocol('NSURLSessionDownloadDelegate')
def URLSession_downloadTask_didResumeAtOffset_expectedTotalBytes_(self,
Expand Down

0 comments on commit b1e5ae8

Please sign in to comment.