Просмотр исходного кода

1.下载过程中全部删除偶现闪退修复
2.下载过程偶现闪退修复

huangxiaodong месяцев назад: 8
Родитель
Сommit
ec10fda80f

+ 9 - 1
创维盒子/双子星云手机/Class/Set/uploadFile/uploadFileView/downloadFileRecordTableView.m

@@ -444,7 +444,15 @@
         return;
     }
     
-    [[nasDownloadFileManager shareInstance] deleteDownloadFileRecordBy:_selectModelArr withDelCache:YES];
+    BOOL isDelAllType = NO;
+    
+    if(_selectModelArr.count >= _downloadingArr.count){
+        isDelAllType = YES;
+    }
+    
+    [[nasDownloadFileManager shareInstance] deleteDownloadFileRecordBy:_selectModelArr withDelCache:YES withAll:isDelAllType];
+    
+    //[[nasDownloadFileManager shareInstance] deleteDownloadFileRecordBy:_selectModelArr withDelCache:YES];
     
     [_selectModelArr removeAllObjects];
     //重新拿数据

+ 3 - 0
创维盒子/双子星云手机/NAS/nasDownloadManager/nasDownloadFileManager.h

@@ -43,6 +43,9 @@ NS_ASSUME_NONNULL_BEGIN
 //删除本地数据库记录
 - (void)deleteDownloadFileRecordBy:(NSMutableArray *)delArr withDelCache:(BOOL)isDelCache;
 
+//删除本地数据库记录
+- (void)deleteDownloadFileRecordBy:(NSMutableArray *)delArr withDelCache:(BOOL)isDelCache withAll:(BOOL)isAllType;
+
 #pragma mark 更新本地数据库记录
 - (void)updataBoxSaveDataInFailBy:(couldPhoneFileModel*)dataModel;
 

+ 23 - 9
创维盒子/双子星云手机/NAS/nasDownloadManager/nasDownloadFileManager.m

@@ -214,8 +214,10 @@ static nasDownloadFileManager * cur_nasDownloadFileManager = nil;
 #pragma mark  处理删除内存数据
 - (void)deteleModel:(couldPhoneFileModel*)delModel inArr:(NSMutableArray*)delArr{
     
-        HLog(@"删除某个指定的下载对象 %@",delModel.name)
+    HLog(@"%ld -删除某个指定的下载对象 %@",delArr.count, delModel.name)
+    if (delArr.count > 0) {
         [delArr removeObject:delModel];
+    }
     
 //    for (couldPhoneFileModel *dataModel in delArr) {
 //        if(delModel.bg_id.integerValue == dataModel.bg_id.integerValue){
@@ -235,7 +237,7 @@ static nasDownloadFileManager * cur_nasDownloadFileManager = nil;
         
         //1.内存数据删除
         [self handleDatabaseArrByDelete:dataModel];
-        
+                
         //2.删除数据量
         NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
         
@@ -248,12 +250,14 @@ static nasDownloadFileManager * cur_nasDownloadFileManager = nil;
             }
         }];
         
-        
-        //3.取消下载
-        NSString *urlString = dataModel.fileUrl;
-        NSString *requestURLEncodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
-        
-        [[nasDownloadManager shareManager] deleteDownloadWithUrl:requestURLEncodedString];
+        if(dataModel.curDownloadStateType == downloadStateWait
+           || dataModel.curDownloadStateType == downloadStateUploading){
+            //3.取消下载
+            NSString *urlString = dataModel.fileUrl;
+            NSString *requestURLEncodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
+            
+            [[nasDownloadManager shareManager] deleteDownloadWithUrl:requestURLEncodedString];
+        }
     }
     
 }
@@ -265,6 +269,16 @@ static nasDownloadFileManager * cur_nasDownloadFileManager = nil;
     [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileRefreshNotification object:nil];
 }
 
+#pragma mark 删除本地数据库记录
+- (void)deleteDownloadFileRecordBy:(NSMutableArray *)delArr withDelCache:(BOOL)isDelCache withAll:(BOOL)isAllType
+{
+    if(isAllType){
+        [[nasDownloadManager shareManager] suspendAllDownloadTask];
+    }
+    
+    [self deleteDownloadFileRecordBy:delArr withDelCache:isAllType];
+}
+
 #pragma mark 更新本地数据库记录
 - (void)updataBoxSaveDataInFailBy:(couldPhoneFileModel*)dataModel
 {
@@ -418,7 +432,7 @@ static nasDownloadFileManager * cur_nasDownloadFileManager = nil;
     if(_databaseArr && _databaseArr.count == 3 ){
         NSMutableArray *ingArr0 = _databaseArr[0];
         NSMutableArray *ingArr = [NSMutableArray arrayWithArray:_databaseArr[0]];
-        HLog(@"%@",ingArr)
+        HLog(@"ingArr --%ld",ingArr.count)
         for (couldPhoneFileModel*dataModel in ingArr) {
             HLog(@"22222222 %@\n%@",urlString,dataModel.fileUrl);
             if([urlString isEqualToString:dataModel.fileUrl]){

+ 55 - 20
创维盒子/双子星云手机/NAS/nasDownloadManager/nasDownloadManager.m

@@ -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;