NSOperation队列行为异常

问题描述:

我有一个一个上传多个图像到服务器的任务。所以我正在使用批处理操作过程。每当我开始上传程序时,一些操作特别是第一个操作在第一个操作开始时就完成了,并且图像没有上传,然后批量上传过程继续正常进行,很少出现其他图像缺失的故障。NSOperation队列行为异常

我使用的代码如下: -

-(void)callWSToUploadRxs{ 


    NSLog(@"the total assets maintained are %lu", (unsigned long)_arr_assetsMaintained.count); 

    NSMutableArray *mutableOperations = [NSMutableArray array]; 
    int imageUploadCount = (int)[self extractFullSizeImagesToUpload].count; 
    // second for loop is to initialize the operations and then queue them. 
    for (int i = 0; i<imageUploadCount; i++) { 


     NSData *imageData = UIImageJPEGRepresentation([_arr_originalImagesToSend objectAtIndex:i],1.0); 

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
     [request setHTTPMethod:@"POST"]; 

     NSLog(@"the url constructed is %@", [NSString stringWithFormat:@"%@/%@/%@/%@",uploadRxUrl,@"4004DD85-1421-4992-A811-8E2F3B2E49F7",@"5293",[_arr_imageNames objectAtIndex:i]]); 
     [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@/%@/%@.jpg",uploadRxUrl,@"4004DD85-1421-4992-A811-8E2F3B2E49F7",@"5293",[_arr_imageNames objectAtIndex:i]]]]; 
     [request setValue:@"binary/octet-stream" forHTTPHeaderField:@"Content-Type"]; 

     [request setHTTPBody:imageData]; 
     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

     [mutableOperations addObject:operation]; 
    } 

    currentUploadIndex++; 
    NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperations progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) { 
     NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations); 

     NSIndexPath * indexOfImageTobeDeleted = [_selectedItemsIndexPaths objectAtIndex:0];//numberOfFinishedOperations-1 
     [_arr_assetsMaintained removeObjectAtIndex:indexOfImageTobeDeleted.item]; 
     [_arr_images removeObjectAtIndex:indexOfImageTobeDeleted.item]; 
     [_arr_fullSizeImages removeObjectAtIndex:indexOfImageTobeDeleted.item]; 
     [_arr_imageNames removeObjectAtIndex:indexOfImageTobeDeleted.item]; 

     if ([_arr_selectedCells containsObject:[NSString stringWithFormat:@"%ld",(long)indexOfImageTobeDeleted.item]] ) 
     { 
      [_arr_selectedCells removeObject:[NSString stringWithFormat:@"%ld",(long)indexOfImageTobeDeleted.item]]; 
      //[cell.img_selctedRxs setHidden:TRUE]; 


     } 
     countBeforeClearingAssets = countBeforeClearingAssets - 1; 
     //Reload the items of UICollectionView performBatchUpdates Block 
     [_albumImagesCollection performBatchUpdates:^{ 
      [_albumImagesCollection deleteItemsAtIndexPaths:@[indexOfImageTobeDeleted]]; 
     } completion:nil]; 

     _selectedItemsIndexPaths = [_albumImagesCollection indexPathsForSelectedItems]; 
     // [_selectedItemsIndexPaths removeObjectAtIndex:0]; 
     NSLog(@"the count of selected items after updation is %lu", (unsigned long)_selectedItemsIndexPaths.count); 


    } completionBlock:^(NSArray *operations) { 
     NSLog(@"All operations in batch complete"); 
     [self callWSToAddNoteForRxs]; 
     [_arr_originalImagesToSend removeAllObjects]; 
     [_arr_selectedCells removeAllObjects]; 
     currentUploadIndex = 0; 
     NSLog(@"the array of image names is %@",_arr_imageNames); 
    }]; 

    [[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO]; 

    // third is to maintain the progress block for each image to be uploaded one after the other. 
    for (AFHTTPRequestOperation *operation in mutableOperations){ 

     [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 

      [_progressOverLayView setAlpha:0.7f]; 
      [_progressView setHidden:FALSE]; 
      [_progressView setProgress: totalBytesWritten*1.0f/totalBytesExpectedToWrite animated: YES]; 
      [_lbl_progressUpdate setHidden:FALSE]; 
      _lbl_progressUpdate.text = [NSString stringWithFormat:@"Image %d of %lu uploading", currentUploadIndex, mutableOperations.count]; 
      NSLog(@"Sent %lld of %lld bytes and progress is %f", totalBytesWritten, totalBytesExpectedToWrite, totalBytesWritten*1.0f/totalBytesExpectedToWrite); 
      if(totalBytesWritten >= totalBytesExpectedToWrite) 
      { 
       //progressView.hidden = YES; 
       [self setComplete]; 
      } 
     }]; 
    } 

} 

在该代码中,得到尽快执行的第一操作我开始上传只有3的4越来越上传的图像即。一个图像总是被忽略。也。如果我在网格中只有图像,则上传成功。

谢谢。

+0

有没有人有一个可行的解决方案呢? –

+0

从上传过程中遗漏的图像是否仍然从albumImagesCollection中删除?而且,这是第一张被遗漏的图像或最后一张? – user2695712

+0

是,它被遗漏并且该图像的完成块运行,实际上它从收集视图中删除它。它主要是两种情况中95%的情况下的第一张图像和4张或更多情况下的2张图像。 Hev你确定了这个问题了吗? –

一些想法...

1)进度更新块。这并不能告诉你哪些操作已完成;只有他们的数量,所以我很怀疑他们可能没有完成代码认为他们是所有的时间...

2)完成块需要一个操作数组....我不知道这是否被所有的操作调用过一次,或者多次使用完成的操作数组调用?你可以看数组的长度来查看。如果它确实被调用了操作子集,那么这将是删除已经上传的资源并进行清理工作的地方,因为您知道哪些操作已完成。

3)在将操作添加到队列之前,我会设置上传进度块;只是为了安全。

4)我无法找到批处理操作方法的文档,并想知道它是否已从最新版本的AFNetworking中移除 - 如果是的话 - 可能是错误的或不好的API?为了清晰起见,我会试图在循环中创建自己的操作;然后做一些状态管理来检查批处理的状态并正确处理。

5)你说一个图像总是被遗漏....它是稳定的 - 总是第一个还是最后一个?在单元网络上或模拟慢/不可靠连接上,它的行为方式是否与上述相同?

+0

1.它指示哪个操作已完成: - currentUploadIndex是变量。 2.完成块只有在所有操作完成后才会执行一次。 3.我应该按照您的建议在代码中保留进度块?你能为你的方法展示一些代码吗? 5.它是非常随机的,但在任何类型的蜂窝网络上表现相同。请提出一条摆脱这一切的方法。 –