nasDownloadManager.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. //
  2. // nasDownloadManager.m
  3. // Private-X
  4. //
  5. // Created by xd h on 2024/8/5.
  6. //
  7. #import "nasDownloadManager.h"
  8. #import "customDownloadOperation.h"
  9. #import "customDownloadCacheManager.h"
  10. @interface nasDownloadManager ()<NSURLSessionDataDelegate>
  11. //排队等候下载的下载地址数组
  12. @property(nonatomic,strong) NSMutableArray *downloadWaitingUrlArr;
  13. //正在下载的下载地址数组
  14. @property(nonatomic,strong) NSMutableArray *downloadingOperationArr;
  15. @property (nonatomic, strong) NSLock *lock;
  16. @end
  17. @implementation nasDownloadManager
  18. + (instancetype)shareManager {
  19. static nasDownloadManager *_instance;
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. _instance = [[self alloc] init];
  23. });
  24. return _instance;
  25. }
  26. - (instancetype)init {
  27. if (self = [super init]) {
  28. _maxDownLoadCount = 1;
  29. _lock = [[NSLock alloc] init];
  30. //[self registeNotification];
  31. }
  32. return self;
  33. }
  34. - (NSString*)uid{
  35. if(!_uid || _uid.length == 0){
  36. return @"customUserName";
  37. }
  38. return _uid;
  39. }
  40. /** 添加要下载的 网络连接 */
  41. - (void)addDownloadWithURLs:(NSArray *)urls{
  42. HLog(@"addDownloadWithURLs")
  43. NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
  44. NSMutableArray *curWaitingUrlArr = [NSMutableArray arrayWithArray:self.downloadWaitingUrlArr];
  45. for (NSString *addUrl in urls) {
  46. BOOL needAddType = YES;
  47. //1. 排查下载中
  48. for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
  49. if([operationDoing.url isEqualToString:addUrl]){
  50. needAddType = NO;
  51. break;
  52. }
  53. }
  54. //1. 排查等待下载
  55. for (NSString *waitUrl in curWaitingUrlArr) {
  56. if([waitUrl isEqualToString:addUrl]){
  57. needAddType = NO;
  58. break;
  59. }
  60. }
  61. if(needAddType){
  62. [self.downloadWaitingUrlArr addObject:addUrl];
  63. }
  64. }
  65. //HLog(@"beginDownload downloadWaitingUrlArr:%@ --- %ld -- \n downloadingOperationArr:%@ ----%ld --%ld --- urls:%@",self.downloadWaitingUrlArr,self.downloadWaitingUrlArr.count,self.downloadingOperationArr,self.downloadingOperationArr.count,_maxDownLoadCount,urls)
  66. //启动下载
  67. [self beginDownload];
  68. }
  69. //在添加下载地址后 启动下载
  70. - (void)beginDownload
  71. {
  72. @synchronized (self) {
  73. //HLog(@"beginDownload downloadWaitingUrlArr:%@ --- %ld -- \n downloadingOperationArr:%@ ----%ld --%ld",self.downloadWaitingUrlArr,self.downloadWaitingUrlArr.count,self.downloadingOperationArr,self.downloadingOperationArr.count,_maxDownLoadCount)
  74. if(self.downloadingOperationArr.count == _maxDownLoadCount){
  75. HLog(@"正在下载的数量达到了最大值 %ld",_maxDownLoadCount)
  76. return;
  77. }
  78. if(self.downloadWaitingUrlArr.count == 0){
  79. HLog(@"没有等待中的下载任务")
  80. return;
  81. }
  82. //判断手机磁盘空间是否有剩余
  83. if(![iTools checkFreeDiskSpaceInBytesIsOK:500*1024*1024]){
  84. mainBlock(^{
  85. [[iToast makeText:NSLocalizedString(@"phone_space_Insufficient",nil)] show];
  86. });
  87. return;
  88. }
  89. NSInteger canAddTaskNumber = _maxDownLoadCount - self.downloadingOperationArr.count;
  90. for (int i=0; i<canAddTaskNumber; i++) {
  91. if(self.downloadWaitingUrlArr.count >= 1){
  92. //创建下载任务
  93. NSString *downloadUrl = self.downloadWaitingUrlArr.firstObject;
  94. NSURLSession *session = [self creatNewSessionFun];
  95. customDownloadOperation * operation = [[customDownloadOperation alloc] initWith:downloadUrl session:session];
  96. [operation.dataTask resume];
  97. //等待下载中的任务
  98. [self.downloadWaitingUrlArr removeObjectAtIndex:0];
  99. //添加到下载中数组
  100. [self.downloadingOperationArr addObject:operation];
  101. }
  102. }
  103. }
  104. }
  105. /** 开始任务(不会自动添加任务,列队中没有就直接返回) 后续改为会自动添加任务 */
  106. - (void)startDownLoadWithUrl:(NSString *)url
  107. {
  108. BOOL needAddType = YES;
  109. HLog(@"startDownLoadWithUrl")
  110. NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
  111. NSMutableArray *curWaitingUrlArr = [NSMutableArray arrayWithArray:self.downloadWaitingUrlArr];
  112. //1. 排查下载中
  113. for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
  114. if([operationDoing.url isEqualToString:url]){
  115. needAddType = NO;
  116. break;
  117. }
  118. }
  119. //1. 排查等待下载
  120. for (NSString *waitUrl in curWaitingUrlArr) {
  121. if([waitUrl isEqualToString:url]){
  122. needAddType = NO;
  123. break;
  124. }
  125. }
  126. if(needAddType){
  127. [self.downloadWaitingUrlArr addObject:url];
  128. }
  129. //启动下载
  130. [self beginDownload];
  131. }
  132. /** 删除任务(删除下载url内容的任务) */
  133. - (void)deleteDownloadWithUrl:(NSString *)url
  134. {
  135. HLog(@"deleteDownloadWithUrl")
  136. NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
  137. NSMutableArray *curWaitingUrlArr = [NSMutableArray arrayWithArray:self.downloadWaitingUrlArr];
  138. //1.检测等待下载的任务
  139. BOOL isWaitingUrlType = NO;
  140. for (NSString*waitingUrl in curWaitingUrlArr) {
  141. if ([waitingUrl isEqualToString:url]) {
  142. [self.downloadWaitingUrlArr removeObject:waitingUrl];
  143. isWaitingUrlType = YES;
  144. break;
  145. }
  146. }
  147. //2.检测下载中的任务
  148. if(!isWaitingUrlType){
  149. for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
  150. if([operationDoing.url isEqualToString:url]){
  151. operationDoing.isManualCancel = YES;
  152. if(operationDoing.dataTask){
  153. [operationDoing.dataTask cancel];
  154. }
  155. // operationDoing.dataTask = nil;
  156. // operationDoing.session = nil;
  157. //
  158. // [operationDoing.handle closeFile];
  159. // operationDoing.handle = nil;
  160. //
  161. // [self.downloadingOperationArr removeObject:operationDoing];
  162. break;
  163. }
  164. }
  165. }
  166. //3. 删除本地文件
  167. [customDownloadCacheManager deleteFileWithUrl:url];
  168. //4.进行下一个任务
  169. //[self beginDownload];
  170. }
  171. /** 暂停任务(暂停下载url内容的任务) */
  172. - (void)supendDownloadWithUrl:(NSString *)url
  173. {
  174. HLog(@"supendDownloadWithUrl")
  175. NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
  176. NSMutableArray *curWaitingUrlArr = [NSMutableArray arrayWithArray:self.downloadWaitingUrlArr];
  177. //1.检测等待下载的任务
  178. for (NSString*waitingUrl in curWaitingUrlArr) {
  179. if ([waitingUrl isEqualToString:url]) {
  180. [self.downloadWaitingUrlArr removeObject:waitingUrl];
  181. break;
  182. }
  183. }
  184. //2.检测下载中的任务
  185. for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
  186. if([operationDoing.url isEqualToString:url]){
  187. operationDoing.isManualCancel = YES;
  188. if(operationDoing.dataTask){
  189. [operationDoing.dataTask cancel];
  190. }
  191. // operationDoing.dataTask = nil;
  192. // operationDoing.session = nil;
  193. //
  194. // [operationDoing.handle closeFile];
  195. // operationDoing.handle = nil;
  196. //
  197. // //删除后 didCompleteWithError 不再处理
  198. // [self.downloadingOperationArr removeObject:operationDoing];
  199. break;
  200. }
  201. }
  202. //进行下一个任务
  203. //[self beginDownload];
  204. }
  205. /** 暂停当前所有的下载任务 下载任务不会从列队中删除 */
  206. - (void)suspendAllDownloadTask
  207. {
  208. HLog(@"suspendAllDownloadTask")
  209. NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
  210. //1.删除等待下载的任务
  211. [self.downloadWaitingUrlArr removeAllObjects];
  212. //2.检测下载中的任务
  213. for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
  214. operationDoing.isManualCancel = YES;
  215. if(operationDoing.dataTask){
  216. [operationDoing.dataTask cancel];
  217. }
  218. // operationDoing.dataTask = nil;
  219. // operationDoing.session = nil;
  220. // [operationDoing.handle closeFile];
  221. // operationDoing.handle = nil;
  222. }
  223. [self.downloadingOperationArr removeAllObjects];
  224. }
  225. #pragma mark 重新启动因为网络失败而停止的任务
  226. - (void)reDownloadNetworkTaskBy:(NSString*)url
  227. {
  228. @synchronized (self) {
  229. [self.downloadWaitingUrlArr insertObject:url atIndex:0];
  230. }
  231. [self beginDownload];
  232. }
  233. #pragma mark - <NSURLSessionDataDelegate>
  234. // ssl 服务 证书信任
  235. - (void)URLSession:(NSURLSession *)session
  236. didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
  237. completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
  238. if(![challenge.protectionSpace.authenticationMethod isEqualToString:@"NSURLAuthenticationMethodServerTrust"]) {
  239. return;
  240. }
  241. // 信任该插件
  242. NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
  243. // 第一个参数 告诉系统如何处置
  244. completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
  245. }
  246. //当请求协议是https的时候回调用该方法
  247. //Challenge 挑战 质询(受保护空间)
  248. //NSURLAuthenticationMethodServerTrust 服务器信任证书
  249. - (void)URLSession:(NSURLSession *)session
  250. task:(NSURLSessionTask *)task
  251. didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
  252. completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler {
  253. if(![challenge.protectionSpace.authenticationMethod isEqualToString:@"NSURLAuthenticationMethodServerTrust"]) {
  254. return;
  255. }
  256. // 信任该插件
  257. NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
  258. // 第一个参数 告诉系统如何处置
  259. completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
  260. }
  261. // 接受到响应调用
  262. - (void)URLSession:(NSURLSession *)session
  263. dataTask:(NSURLSessionDataTask *)dataTask
  264. didReceiveResponse:(NSURLResponse *)response
  265. completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
  266. // 将响应交给列队处理
  267. BOOL canDownload = [self handleOperateby:dataTask WithResponse:response];
  268. if(canDownload){
  269. // 允许下载
  270. completionHandler(NSURLSessionResponseAllow);
  271. }
  272. else{
  273. // 不允许下载
  274. completionHandler(NSURLSessionResponseCancel);
  275. }
  276. }
  277. // 接受到数据碎片 的时候调用,调用多次
  278. - (void)URLSession:(NSURLSession *)session
  279. dataTask:(NSURLSessionDataTask *)dataTask
  280. didReceiveData:(NSData *)data {
  281. // 接收到session 下载碎片交个列队管理
  282. [self dataTask:dataTask didReceiveData:data];
  283. }
  284. // <NSURLSessionDataDelegate> 完成下载
  285. - (void)URLSession:(NSURLSession *)session
  286. task:(NSURLSessionTask *)task
  287. didCompleteWithError:(nullable NSError *)error {
  288. [self task:task didCompleteWithError:error];
  289. }
  290. - (void)URLSession:(NSURLSession *)session
  291. task:(NSURLSessionTask *)task
  292. needNewBodyStream:(void (^)(NSInputStream * _Nullable bodyStream))completionHandler {
  293. }
  294. - (void)URLSession:(NSURLSession *)session
  295. task:(NSURLSessionTask *)task
  296. didSendBodyData:(int64_t)bytesSent
  297. totalBytesSent:(int64_t)totalBytesSent
  298. totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
  299. }
  300. #pragma mark 处理接收到的数据
  301. // 接收到相应时
  302. - (BOOL)handleOperateby:(NSURLSessionTask*)dataTask WithResponse:(NSURLResponse *)response {
  303. HLog(@"handleOperateby")
  304. customDownloadOperation *operation = nil;
  305. NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
  306. HLog(@"handleOperateby 开始遍历");
  307. for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
  308. if(operationDoing.dataTask == dataTask){
  309. operation = operationDoing;
  310. break;
  311. }
  312. }
  313. if (!operation) {
  314. HLog(@"没找到当前下载任务");
  315. operation.downloadState = customDownloadStateFailed;
  316. [dataTask cancel];
  317. return NO;
  318. }
  319. // 检查response是否是NSHTTPURLResponse的实例
  320. if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
  321. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
  322. NSInteger statusCode = httpResponse.statusCode;
  323. HLog(@"HTTP Status Code: %ld", (long)statusCode);
  324. if(statusCode == 404){
  325. operation.downloadState = customDownloadStateFailed;
  326. operation.isFile404Cancel = YES;
  327. return NO;
  328. }
  329. }
  330. // 总的size
  331. if (operation.currentSize + response.expectedContentLength == 0) {
  332. HLog(@"下载数据回调异常");
  333. operation.downloadState = customDownloadStateFailed;
  334. return NO;
  335. }
  336. operation.totalSize = operation.currentSize + response.expectedContentLength;
  337. //判断手机磁盘空间是否有剩余
  338. if(![iTools checkFreeDiskSpaceInBytesIsOK:operation.totalSize + 500*1024*1024]){
  339. operation.downloadState = customDownloadStateFailed;
  340. [dataTask cancel];
  341. mainBlock(^{
  342. [[iToast makeText:NSLocalizedString(@"phone_space_Insufficient",nil)] show];
  343. });
  344. return NO;
  345. }
  346. // 创建空的文件夹
  347. if (operation.currentSize == 0) {
  348. // 创建空的文件
  349. [[NSFileManager defaultManager] createFileAtPath:operation.fullPath contents:nil attributes:nil];
  350. }
  351. // 创建文件句柄
  352. operation.handle = [NSFileHandle fileHandleForWritingAtPath:operation.fullPath];
  353. // 文件句柄移动到文件末尾 位置 // 返回值是 unsign long long
  354. [operation.handle seekToEndOfFile];
  355. // 开始下载记录文件下载信息
  356. operation.downloadState = customDownloadStateDoing;
  357. return YES;
  358. }
  359. - (void)dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
  360. {
  361. HLog(@"didReceiveData")
  362. customDownloadOperation *operation = [self checkCurrentOperationByDataTask:dataTask withNeedDelete:NO];
  363. // NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
  364. // HLog(@"didReceiveData 开始遍历");
  365. // for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
  366. // if(operationDoing.dataTask == dataTask){
  367. // operation = operationDoing;
  368. // break;
  369. // }
  370. // }
  371. if (!operation) {
  372. HLog(@"没找到当前下载任务");
  373. [dataTask cancel];
  374. return;
  375. }
  376. //手动取消任务
  377. if (operation.isManualCancel) {
  378. return;
  379. }
  380. // 获得已经下载的文件大小
  381. operation.currentSize += data.length;
  382. HLog(@"当前线程:%@ -----currentSize:%lld---progress:%.2f",[NSThread currentThread], operation.currentSize, 1.00*operation.currentSize/operation.totalSize);
  383. // 写入文件
  384. if(operation.handle){
  385. [operation.handle writeData:data];
  386. }
  387. // 下载中通知
  388. [self operationDoningWithOperation:operation];
  389. }
  390. - (void)task:(NSURLSessionTask *)dataTask didCompleteWithError:(NSError *)error
  391. {
  392. HLog(@"didCompleteWithError")
  393. customDownloadOperation *operation = [self checkCurrentOperationByDataTask:dataTask withNeedDelete:YES];
  394. // NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
  395. // HLog(@"didCompleteWithError 开始遍历");
  396. // for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
  397. // if(operationDoing.dataTask == dataTask){
  398. // operation = operationDoing;
  399. // [self.downloadingOperationArr removeObject:operationDoing];
  400. // break;
  401. // }
  402. // }
  403. if (!operation) {
  404. HLog(@"没找到当前下载任务");
  405. //[dataTask suspend];
  406. return;
  407. }
  408. if(operation.dataTask){
  409. //[operation.dataTask cancel];
  410. operation.dataTask = nil;
  411. operation.session = nil;
  412. if(operation.handle){
  413. // 关闭文件句柄
  414. [operation.handle closeFile];
  415. // 释放文件句柄
  416. operation.handle = nil;
  417. }
  418. }
  419. // -1005 网络中断 1009 网络似乎中断
  420. if(error && (error.code == -1005 || error.code == -1009) && !operation.isFile404Cancel){//网络中断
  421. HLog(@"reDownloadNetworkTaskBy");
  422. //延时几秒再次启动这个任务
  423. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  424. // [self reDownloadNetworkTaskBy:operation.url];
  425. // });
  426. [[nasDownloadFileManager shareInstance] saveDownloadloadingTaskByNetWorkErrorFun];
  427. return;
  428. }
  429. if(operation.isManualCancel){
  430. [self beginDownload];
  431. return;
  432. }
  433. // 完成下载 通知 block
  434. if (error ||operation.downloadState == customDownloadStateFailed) {
  435. [self operationFailedWithOperation:operation];
  436. } else {
  437. operation.isFinished = YES;
  438. operation.timeStamp = [iTools getNowTimeStamp];
  439. [self operationSuccessWithOperation:operation];
  440. }
  441. [self beginDownload];
  442. }
  443. #pragma mark 查询正在操作的 operation 并且看是否要删除
  444. - (customDownloadOperation *)checkCurrentOperationByDataTask:(NSURLSessionTask *)dataTask withNeedDelete:(BOOL)needDelete
  445. {
  446. customDownloadOperation *operation = nil;
  447. [_lock lock];
  448. NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
  449. HLog(@"开始遍历 查询当前下载任务");
  450. for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
  451. if(operationDoing.dataTask == dataTask){
  452. operation = operationDoing;
  453. if(needDelete){
  454. [self.downloadingOperationArr removeObject:operationDoing];
  455. }
  456. break;
  457. }
  458. }
  459. HLog(@"结束遍历 查询当前下载任务");
  460. [_lock unlock];
  461. return operation;
  462. }
  463. #pragma mark 发送通知
  464. // 失败某一个operation 保存本地 通知外界
  465. - (void)operationFailedWithOperation:(customDownloadOperation *)operation {
  466. HLog(@"DownloadTaskExeError \n %@",operation.url);
  467. operation.downloadState = customDownloadStateFailed;
  468. [[NSNotificationCenter defaultCenter] postNotificationName:nasDownloadTaskExeError object:nil userInfo:@{@"operation" : operation}];
  469. }
  470. // 重启某一个operation 保存本地 通知外界
  471. - (void)operationDoningWithOperation:(customDownloadOperation *)operation {
  472. HLog(@"DownloadTaskExeing");
  473. NSTimeInterval curTime = [[NSDate date] timeIntervalSince1970];
  474. NSTimeInterval timeDiff = curTime - operation.preNotTimeInterval;
  475. HLog(@"控制刷新时间为1秒:%f",timeDiff);
  476. CGFloat RefreshTimer = 0.8;
  477. if(operation.totalSize <= 5*1024*1024){//小于5M 事实
  478. RefreshTimer = 0.2;
  479. }
  480. if (operation.preNotTimeInterval <= 0
  481. || timeDiff > RefreshTimer ) {
  482. HLog(@"下载发送刷新通知:%f",timeDiff);
  483. operation.preNotTimeInterval = curTime;
  484. [[NSNotificationCenter defaultCenter] postNotificationName:nasDownloadTaskExeing object:nil userInfo:@{@"operation" : operation}];
  485. }
  486. }
  487. // 等待某一个operation 保存本地 通知外界
  488. - (void)operationSuccessWithOperation:(customDownloadOperation *)operation {
  489. HLog(@"DownloadTaskExeEnd \n %@",operation.url);
  490. operation.downloadState = customDownloadStateCompleted;
  491. [[NSNotificationCenter defaultCenter] postNotificationName:nasDownloadTaskExeEnd object:nil userInfo:@{@"operation" : operation}];
  492. }
  493. -(NSURLSession*)creatNewSessionFun
  494. {
  495. NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
  496. // 设置请求超时
  497. config.timeoutIntervalForRequest = -1;
  498. // config.networkServiceType = NSURLNetworkServiceTypeVideo;
  499. config.timeoutIntervalForResource = -1;
  500. // config.TLSMaximumSupportedProtocol = kSSLProtocolAll;
  501. //_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue currentQueue]];
  502. NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
  503. return session;
  504. }
  505. #pragma mark - lazy load
  506. - (NSMutableArray *)downloadWaitingUrlArr {
  507. if (!_downloadWaitingUrlArr) {
  508. _downloadWaitingUrlArr = [NSMutableArray array];
  509. }
  510. return _downloadWaitingUrlArr;
  511. }
  512. - (NSMutableArray *)downloadingOperationArr {
  513. if (!_downloadingOperationArr) {
  514. _downloadingOperationArr = [NSMutableArray array];
  515. }
  516. return _downloadingOperationArr;
  517. }
  518. - (BOOL)isDownLoadIngType
  519. {
  520. if(self.downloadWaitingUrlArr.count >= 1){
  521. return YES;
  522. }
  523. if(self.downloadingOperationArr.count >= 1){
  524. return YES;
  525. }
  526. return NO;
  527. }
  528. @end