iOS開發之斷點下載時session造成的內存泄漏


然后是相應的注意點 以及一個 斷點下載的小例子

 

 

[objc]  view plain  copy
  1. 注意點   
  2.  > 1. 在下載完成之后需要對URLSession 做finishTasksAndInvalidate操作;   
  3.  >    或者進行invalidateAndCancel 操作也行  
  4.  > 2. 下載的文件保存再temp問價夾中下載完成后會自動刪除,需要再下載完成的時候自行進行處理  
  5.  > 3.  一旦對session發送了invalidateAndCancel消息,session就再也無法發起任務了!  
  6.   > 4. 在處理臨時文件的時候調用[[NSFileManager defaultManager] copyItemAtPath:tempPath   toPath:cachePath error:NULL]; 可以解決內存飆升問題  
  7.  >5.  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController () <NSURLSessionDownloadDelegate >  
  12. @property (nonatomic, strong) NSURLSession *session;  
  13. @end  
  14.   
  15. @implementation ViewController  
  16. /** 
  17.  重要 
  18.  session對象,會對代理進行強引用,如果不調用invalidateAndCancel取消    session,會出現內存泄漏 
  19.   官方文檔中的說名 
  20.  The session object keeps a strong reference to the delegate until your app explicitly invalidates the session. If you do not invalidate the session by calling the invalidateAndCancel or resetWithCompletionHandler: method, your app leaks memory. 
  21.  */  
  22.   
  23. - (NSURLSession *)session {  
  24.     if (_session == nil) {  
  25.         // session 是為了方便程序員使用的全局的單例,如果要通過代理跟進下載進度,需要自己實例化一個session  
  26.         //    NSURLSession *session = [NSURLSession sharedSession];  
  27.         // config可以配置session,指定session中的超時時長,緩存策略,安全憑據,cookie...  
  28.         // 可以保證全局共享,統一在一個網絡會話中使用!  
  29.         NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];  
  30.          
  31.         /** 
  32.          隊列:如果指定nil,會默認使用異步執行隊列的代理方法 
  33.          因為:所有網絡操作默認都是異步的,因此不指定隊列,就是異步的 
  34.          */  
  35.         //    NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[[NSOperationQueue alloc] init]];  
  36.         _session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];  
  37.     }  
  38.     return _session;  
  39. }  
  40.   
  41. - (void)dealloc {  
  42.     [self.session invalidateAndCancel];  
  43.      
  44.     NSLog(@"我去了");  
  45. }  
  46.   
  47. /** 
  48.  跟蹤下載進度 
  49.  */  
  50. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
  51.   
  52.     // url  
  53.     NSString *urlString = @"http://127.0.0.1/01.C語言-語法預覽.mp4";  
  54.     urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
  55.     NSURL *url = [NSURL URLWithString:urlString];  
  56.      
  57.     // 數據任務  
  58.     // 如果要跟蹤下載進度,在session中,同樣是需要代理  
  59.     [[self.session downloadTaskWithURL:url] resume];  
  60.      
  61. }  
  62.   
  63. #pragma mark - 下載的代理方法  
  64. /** 下載完成 */  
  65. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask  
  66. didFinishDownloadingToURL:(NSURL *)location {  
  67.     NSLog(@"+++%@", location);  
  68.     /***********************************************/  
  69.     // 完成任務 完成的時候需要進行釋放否則會造成內存泄露  
  70.     [self.session finishTasksAndInvalidate];  
  71. }  
  72.   
  73. // 以下兩個方法,在iOS7中,是必須的  
  74. // ios8中變成了可選的  
  75. /** 
  76.  bytesWritten                   本次下載字節數 
  77.  totalBytesWritten              已經下載字節數 
  78.  totalBytesExpectedToWrite      總下載字節數(文件大小) 
  79.  */  
  80. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask  
  81.       didWriteData:(int64_t)bytesWritten  
  82.  totalBytesWritten:(int64_t)totalBytesWritten  
  83. totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {  
  84.   
  85.     // 進度  
  86.     float progress = (float) totalBytesWritten / totalBytesExpectedToWrite;  
  87.     NSLog(@"%f %@", progress, [NSThread currentThread]);  
  88. }  
  89.   
  90. // 斷點續傳的  
  91. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask  
  92.  didResumeAtOffset:(int64_t)fileOffset  
  93. expectedTotalBytes:(int64_t)expectedTotalBytes {  
  94.     // 通常不用寫任何東西  
  95. }  
  96. @end  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM