|
@@ -14,6 +14,8 @@
|
|
|
@property(nonatomic,strong) NSMutableArray *downloadWaitingUrlArr;
|
|
|
//正在下载的下载地址数组
|
|
|
@property(nonatomic,strong) NSMutableArray *downloadingOperationArr;
|
|
|
+
|
|
|
+@property (nonatomic, strong) NSLock *lock;
|
|
|
@end
|
|
|
|
|
|
@implementation nasDownloadManager
|
|
@@ -30,6 +32,7 @@
|
|
|
|
|
|
if (self = [super init]) {
|
|
|
_maxDownLoadCount = 1;
|
|
|
+ _lock = [[NSLock alloc] init];
|
|
|
//[self registeNotification];
|
|
|
}
|
|
|
return self;
|
|
@@ -45,7 +48,7 @@
|
|
|
|
|
|
/** 添加要下载的 网络连接 */
|
|
|
- (void)addDownloadWithURLs:(NSArray *)urls{
|
|
|
-
|
|
|
+ HLog(@"addDownloadWithURLs")
|
|
|
NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
|
|
|
|
|
|
NSMutableArray *curWaitingUrlArr = [NSMutableArray arrayWithArray:self.downloadWaitingUrlArr];
|
|
@@ -134,7 +137,7 @@
|
|
|
{
|
|
|
|
|
|
BOOL needAddType = YES;
|
|
|
-
|
|
|
+ HLog(@"startDownLoadWithUrl")
|
|
|
NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
|
|
|
|
|
|
NSMutableArray *curWaitingUrlArr = [NSMutableArray arrayWithArray:self.downloadWaitingUrlArr];
|
|
@@ -167,6 +170,7 @@
|
|
|
/** 删除任务(删除下载url内容的任务) */
|
|
|
- (void)deleteDownloadWithUrl:(NSString *)url
|
|
|
{
|
|
|
+ HLog(@"deleteDownloadWithUrl")
|
|
|
NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
|
|
|
|
|
|
NSMutableArray *curWaitingUrlArr = [NSMutableArray arrayWithArray:self.downloadWaitingUrlArr];
|
|
@@ -212,6 +216,7 @@
|
|
|
/** 暂停任务(暂停下载url内容的任务) */
|
|
|
- (void)supendDownloadWithUrl:(NSString *)url
|
|
|
{
|
|
|
+ HLog(@"supendDownloadWithUrl")
|
|
|
NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
|
|
|
|
|
|
NSMutableArray *curWaitingUrlArr = [NSMutableArray arrayWithArray:self.downloadWaitingUrlArr];
|
|
@@ -253,6 +258,7 @@
|
|
|
/** 暂停当前所有的下载任务 下载任务不会从列队中删除 */
|
|
|
- (void)suspendAllDownloadTask
|
|
|
{
|
|
|
+ HLog(@"suspendAllDownloadTask")
|
|
|
NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
|
|
|
|
|
|
//1.删除等待下载的任务
|
|
@@ -374,8 +380,10 @@ totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
|
|
|
// 接收到相应时
|
|
|
- (BOOL)handleOperateby:(NSURLSessionTask*)dataTask WithResponse:(NSURLResponse *)response {
|
|
|
|
|
|
+ HLog(@"handleOperateby")
|
|
|
customDownloadOperation *operation = nil;
|
|
|
NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
|
|
|
+ HLog(@"handleOperateby 开始遍历");
|
|
|
for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
|
|
|
if(operationDoing.dataTask == dataTask){
|
|
|
operation = operationDoing;
|
|
@@ -444,14 +452,16 @@ totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
|
|
|
|
|
|
- (void)dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
|
|
|
{
|
|
|
- customDownloadOperation *operation = nil;
|
|
|
- NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
|
|
|
- for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
|
|
|
- if(operationDoing.dataTask == dataTask){
|
|
|
- operation = operationDoing;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
+ HLog(@"didReceiveData")
|
|
|
+ customDownloadOperation *operation = [self checkCurrentOperationByDataTask:dataTask withNeedDelete:NO];
|
|
|
+// NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
|
|
|
+// HLog(@"didReceiveData 开始遍历");
|
|
|
+// for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
|
|
|
+// if(operationDoing.dataTask == dataTask){
|
|
|
+// operation = operationDoing;
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// }
|
|
|
|
|
|
if (!operation) {
|
|
|
HLog(@"没找到当前下载任务");
|
|
@@ -479,15 +489,17 @@ totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
|
|
|
|
|
|
- (void)task:(NSURLSessionTask *)dataTask didCompleteWithError:(NSError *)error
|
|
|
{
|
|
|
- customDownloadOperation *operation = nil;
|
|
|
- NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
|
|
|
- for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
|
|
|
- if(operationDoing.dataTask == dataTask){
|
|
|
- operation = operationDoing;
|
|
|
- [self.downloadingOperationArr removeObject:operationDoing];
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
+ HLog(@"didCompleteWithError")
|
|
|
+ customDownloadOperation *operation = [self checkCurrentOperationByDataTask:dataTask withNeedDelete:YES];
|
|
|
+// NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
|
|
|
+// HLog(@"didCompleteWithError 开始遍历");
|
|
|
+// for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
|
|
|
+// if(operationDoing.dataTask == dataTask){
|
|
|
+// operation = operationDoing;
|
|
|
+// [self.downloadingOperationArr removeObject:operationDoing];
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// }
|
|
|
|
|
|
if (!operation) {
|
|
|
HLog(@"没找到当前下载任务");
|
|
@@ -534,6 +546,29 @@ totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
|
|
|
[self beginDownload];
|
|
|
}
|
|
|
|
|
|
+#pragma mark 查询正在操作的 operation 并且看是否要删除
|
|
|
+- (customDownloadOperation *)checkCurrentOperationByDataTask:(NSURLSessionTask *)dataTask withNeedDelete:(BOOL)needDelete
|
|
|
+{
|
|
|
+ customDownloadOperation *operation = nil;
|
|
|
+ [_lock lock];
|
|
|
+
|
|
|
+ NSMutableArray *curDownloadingOperationArr = [NSMutableArray arrayWithArray:self.downloadingOperationArr];
|
|
|
+ HLog(@"开始遍历 查询当前下载任务");
|
|
|
+ for (customDownloadOperation *operationDoing in curDownloadingOperationArr) {
|
|
|
+ if(operationDoing.dataTask == dataTask){
|
|
|
+ operation = operationDoing;
|
|
|
+ if(needDelete){
|
|
|
+ [self.downloadingOperationArr removeObject:operationDoing];
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ HLog(@"结束遍历 查询当前下载任务");
|
|
|
+
|
|
|
+ [_lock unlock];
|
|
|
+
|
|
|
+ return operation;
|
|
|
+}
|
|
|
|
|
|
#pragma mark 发送通知
|
|
|
|
|
@@ -556,7 +591,7 @@ totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
|
|
|
HLog(@"控制刷新时间为1秒:%f",timeDiff);
|
|
|
|
|
|
if (operation.preNotTimeInterval <= 0
|
|
|
- || timeDiff > 0.8 ) {
|
|
|
+ || timeDiff > 0.5 ) {
|
|
|
|
|
|
HLog(@"下载发送刷新通知:%f",timeDiff);
|
|
|
operation.preNotTimeInterval = curTime;
|