浏览代码

1.上传bug修复

huangxiaodong 1 年之前
父节点
当前提交
522dc1cc94

+ 131 - 104
创维盒子/双子星云手机/NAS/nasUploadManager/nasMixUploadManager.m

@@ -19,6 +19,7 @@
 //正在下载的上传地址数组
 @property(nonatomic,strong) NSMutableArray *uploadingOperationArr;
 
+@property(nonatomic,strong)NSLock  *arrayLock;//数组遍历是修改会挂掉
 @end
 
 @implementation nasMixUploadManager
@@ -36,6 +37,7 @@
     if (self = [super init]) {
         _maxUploadLoadCount = 2;
         
+        _arrayLock = [NSLock new];
         //[self registeNotification];
     }
     return self;
@@ -77,6 +79,7 @@
 //在添加XX后 启动下载
 - (void)beginUploadAfterDeleteOperationBy:(customUploadOperation*)operation
 {
+    HLog(@"beginUploadAfterDeleteOperationBy")
     BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular_all)];
     if(!isCanUseCellular){//不允许流量上传
         //
@@ -100,48 +103,50 @@
         return;
     }
     
-    @synchronized (self) {
-        
-        if(operation){
-            [self.uploadingOperationArr removeObject:operation];
-        }
-        
-        if(self.uploadingOperationArr.count == _maxUploadLoadCount){
-            HLog(@"正在上传的数量达到了最大值 %ld",_maxUploadLoadCount)
-            return;
-        }
-        
-        if(self.uploadWaitingUrlArr.count == 0){
-            HLog(@"没有等待中的上传任务")
-            return;
-        }
-        
-        NSInteger canAddTaskNumber = _maxUploadLoadCount - self.uploadingOperationArr.count;
+    
+    if(operation){
+        HLog(@"删除完成的上传任务")
+        [_arrayLock lock];
+        [self.uploadingOperationArr removeObject:operation];
+        [_arrayLock unlock];
+    }
+    
+    if(self.uploadingOperationArr.count == _maxUploadLoadCount){
+        HLog(@"正在上传的数量达到了最大值 %ld",_maxUploadLoadCount)
+        return;
+    }
+    
+    if(self.uploadWaitingUrlArr.count == 0){
+        HLog(@"没有等待中的上传任务")
+        return;
+    }
+    
+    NSInteger canAddTaskNumber = _maxUploadLoadCount - self.uploadingOperationArr.count;
+    
+    [_arrayLock lock];
+    for (int i=0; i<canAddTaskNumber; i++) {
         
-        for (int i=0; i<canAddTaskNumber; i++) {
+        if(self.uploadWaitingUrlArr.count >= 1){
+            //创建上传任务
+            uploadFileDataModel *WaitingModel = self.uploadWaitingUrlArr.firstObject;
             
-            if(self.uploadWaitingUrlArr.count >= 1){
-                //创建上传任务
-                uploadFileDataModel *WaitingModel = self.uploadWaitingUrlArr.firstObject;
-                
-                __block customUploadOperation * operation =  [customUploadOperation new];
-                operation.fileModel = WaitingModel;
-                
-                //等待下载中的任务
-                [self.uploadWaitingUrlArr removeObjectAtIndex:0];
-                
-                HLog(@"添加一个新的上传任务")
-                //添加到下载中数组
-                [self.uploadingOperationArr addObject:operation];
-                
-                [self checkFileUploadStateWithOperation:operation];
-                
-                //[weakSelf handleTaskDidUploadWith:operation withState:state];
-            }
+            __block customUploadOperation * operation =  [customUploadOperation new];
+            operation.fileModel = WaitingModel;
+            
+            //等待下载中的任务
+            [self.uploadWaitingUrlArr removeObjectAtIndex:0];
+            
+            HLog(@"添加一个新的上传任务")
+            //添加到下载中数组
+            [self.uploadingOperationArr addObject:operation];
+            
+            [self checkFileUploadStateWithOperation:operation];
             
+            //[weakSelf handleTaskDidUploadWith:operation withState:state];
         }
-    
+        
     }
+    [_arrayLock unlock];
     
 }
 
@@ -194,18 +199,7 @@
     else if(model.data.isComplete){//上传过了 并且文件上传完了
         //判断下文件创建长度是否一致 一致则是上传完了 不一致 重新上传一个 可能是同名的文件而已
         if(model.data.size >= operation.fileModel.totalBytes){//上传完了
-            mainBlock(^{
-                [[iToast makeText:NSLocalizedString(@"File_upload_file_already_exists",nil)] show];
-            });
-            
-            NSMutableArray *delArr = [NSMutableArray new];
-            [delArr addObject:operation.fileModel];
-            
-            [[nasUploadFileManager shareInstance] deleteUploadFileRecordBy:delArr withDelCache:YES complete:^(BOOL isSuccess) {
-                if (isSuccess) {
-                    
-                }
-            }];
+            [self handleFileDidUploadFunWithOperation:operation];
         }
         else{//未上传完 isComplete 这个字段有问题
             operation.fileModel.didUploadBytes = model.data.size;
@@ -220,7 +214,22 @@
 }
 
 
-#pragma mark 处理任务失败
+#pragma mark 任务上传过了了
+- (void)handleFileDidUploadFunWithOperation:(customUploadOperation*)operation
+{
+    mainBlock(^{
+        [[iToast makeText:NSLocalizedString(@"File_upload_file_already_exists",nil)] show];
+    });
+    
+    NSMutableArray *delArr = [NSMutableArray new];
+    [delArr addObject:operation.fileModel];
+    
+    [[nasUploadFileManager shareInstance] deleteUploadFileRecordBy:delArr withDelCache:YES complete:^(BOOL isSuccess) {
+        if (isSuccess) {
+            
+        }
+    }];
+}
 
 
 #pragma mark 准备上传文件
@@ -263,6 +272,16 @@
             NSString*pathStr = [cachesFileManager getFilePathWithName:operation.fileModel.filename type:operation.fileModel.curUploadFileType];
             NSData *imageData = [NSData dataWithContentsOfFile:pathStr];
             
+            //修复出现过 图片数据小于totalBytes
+            if(imageData){
+                long curDataLengt = [imageData length];
+                if(curDataLengt == operation.fileModel.didUploadBytes && curDataLengt < operation.fileModel.totalBytes){
+                    HLog(@"totalBytes 获取出错?")
+                    [self handleFileDidUploadFunWithOperation:operation];
+                    return;
+                }
+            }
+            
             if (!imageData) {
                 
                 if(!operation.fileModel.asset){
@@ -282,6 +301,7 @@
                             [weakSelf  afterGetImageDataFunWithOperation:operation];
                         }
                         else{
+                            HLog(@"\n\n\n\nrequestImageDataForAsset fail imageData nil\n\n\n")
                             [weakSelf handleUploadFailOneFileBy:operation];
                         }
                 }];
@@ -482,6 +502,7 @@
 #pragma mark 处理上传完成
 - (void)handleUploadDoneOneFileBy:(customUploadOperation*)operation
 {
+    HLog(@"handleUploadDoneOneFileBy")
     [[NSNotificationCenter defaultCenter] postNotificationName:nasUploadTaskExeEnd object:operation.fileModel];
     [self beginUploadAfterDeleteOperationBy:operation];
 }
@@ -749,77 +770,83 @@ didReceiveResponse:(NSURLResponse *)response
     HLog(@"didSendBodyData: %lld--%lld-%lld", bytesSent, totalBytesSent, totalBytesExpectedToSend);
     
     //最后一片数据不刷新
-    if(totalBytesSent == totalBytesExpectedToSend){
-        return;
-    }
-    
-    @synchronized (self) {
-        NSMutableArray *preUploadingOperationArr = [_uploadingOperationArr copy];
-        for (customUploadOperation*operation in preUploadingOperationArr) {
-            if(operation.dataTask == task){
-                operation.fileModel.didUploadBytes += bytesSent;
-                
-                //调整下大小 因为上传的数据 除了data 长度 还包含了 参数大小
-                if(totalBytesSent == totalBytesExpectedToSend){
-                    operation.fileModel.didUploadBytes -= (totalBytesExpectedToSend - operation.onceDataLengt);
-                }
-                
-                NSTimeInterval curTime = [[NSDate date] timeIntervalSince1970];
-                NSTimeInterval timeDiff = curTime - operation.preNotTimeInterval;
-                
-                //HLog(@"控制刷新时间为1秒:%f",timeDiff);
-                
-                if (operation.preNotTimeInterval <= 0
-                    || timeDiff > 0.8 ) {
-                    
-                    operation.preNotTimeInterval = curTime;
-                    [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:operation.fileModel];
-                }
-                
-                break;
-            }
-        }
-    }
+//    if(totalBytesSent == totalBytesExpectedToSend){
+//        return;
+//    }
+    
+    
+//    [_arrayLock lock];
+//    for (customUploadOperation*operation in _uploadingOperationArr) {
+//        if(operation.dataTask == task){
+//            operation.fileModel.didUploadBytes += bytesSent;
+//            
+//            //调整下大小 因为上传的数据 除了data 长度 还包含了 参数大小
+//            if(totalBytesSent == totalBytesExpectedToSend){
+//                operation.fileModel.didUploadBytes -= (totalBytesExpectedToSend - operation.onceDataLengt);
+//            }
+//            
+//            NSTimeInterval curTime = [[NSDate date] timeIntervalSince1970];
+//            NSTimeInterval timeDiff = curTime - operation.preNotTimeInterval;
+//            
+//            //HLog(@"控制刷新时间为1秒:%f",timeDiff);
+//            
+//            if (operation.preNotTimeInterval <= 0
+//                || timeDiff > 0.8 ) {
+//                
+//                operation.preNotTimeInterval = curTime;
+//                [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:operation.fileModel];
+//            }
+//            
+//            break;
+//        }
+//    }
+//    [_arrayLock unlock];
 }
 
 #pragma mark 取消单个任务
 - (void)cancelUploadTaskFunWith:(uploadFileDataModel*)fileModel
 {
     HLog(@"取消任务-- %@--%@",fileModel.filename,fileModel.taskId)
-    @synchronized (self) {
-        for (uploadFileDataModel*waitModel in _uploadWaitingUrlArr) {
-            if([waitModel.filename isEqualToString:fileModel.filename]){
-                [_uploadWaitingUrlArr removeObject:waitModel];
-                return;
-            }
+    
+    [_arrayLock lock];
+    
+    for (customUploadOperation*operation in _uploadingOperationArr) {
+        if([operation.fileModel.filename isEqualToString:fileModel.filename]){
+            operation.isCancelType = YES;
+            [operation.dataTask cancel];
+            [_arrayLock unlock];
+            [self beginUploadAfterDeleteOperationBy:operation];
+            return;
         }
-        
-        for (customUploadOperation*operation in _uploadingOperationArr) {
-            if([operation.fileModel.filename isEqualToString:fileModel.filename]){
-                operation.isCancelType = YES;
-                [operation.dataTask cancel];
-                [self beginUploadAfterDeleteOperationBy:operation];
-                break;
-            }
+    }
+    
+    for (uploadFileDataModel*waitModel in _uploadWaitingUrlArr) {
+        if([waitModel.filename isEqualToString:fileModel.filename]){
+            [_uploadWaitingUrlArr removeObject:waitModel];
+            [_arrayLock lock];
+            [self beginUploadAfterDeleteOperationBy:nil];
+            return;
         }
     }
     
+    [_arrayLock unlock];
+    
 }
 
 #pragma mark 取消所有任务
 - (void)cancelUploadAllTaskFun
 {
     HLog(@"取消所有任务")
-    @synchronized (self) {
-        [_uploadWaitingUrlArr removeAllObjects];
-        
-        for (customUploadOperation*operation in _uploadingOperationArr) {
-            operation.isCancelType = YES;
-            [operation.dataTask cancel];
-        }
-        
-        [_uploadingOperationArr removeAllObjects];
+    [_arrayLock lock];
+    [_uploadWaitingUrlArr removeAllObjects];
+    
+    for (customUploadOperation*operation in _uploadingOperationArr) {
+        operation.isCancelType = YES;
+        [operation.dataTask cancel];
     }
+    
+    [_uploadingOperationArr removeAllObjects];
+    [_arrayLock unlock];
 }
 
 

+ 1 - 1
创维盒子/双子星云手机/netWork/netWorkManager.m

@@ -143,7 +143,7 @@ static netWorkManager *Game_NetWorkShareInstance = nil;
     
     // 设置超时时间(设置有效)
     //[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
-    manager.requestSerializer.timeoutInterval = 30;
+    manager.requestSerializer.timeoutInterval = 180;
     //[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
     
 }