我的任务是逐个将多个图像上传到服务器.所以我正在使用批量操作流程.每次我开始上传程序时,一些操作特别是第一个操作在它启动时就完成并且图像没有上传,然后批量上载过程继续正常,并且遗漏了其他图像的罕见故障.
我使用的代码如下: –
-(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",[_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]; } }]; } }
在这段代码中,第一个操作是在我开始上传图像后立即执行,即只有4个中的4个上传.总是遗漏一张图片.也.如果我在网格中只有Image,则上传成功.
谢谢.
解决方法
一些想法……
1)进度更新块.这并不能告诉你哪些操作已经完成;只有他们的数量,所以我怀疑他们可能没有按照代码认为他们一直在的顺序完成….
2)完成块采用一系列操作….我想知道是否所有操作都调用了一次,或者多次完成了操作数组?您可以查看数组长度以查看.如果确实调用了操作子集,那么这将是删除已经上载的资产并执行清理工作的地方,因为您知道哪些操作已完成.
3)我会在将操作添加到队列之前设置上传进度块;只是为了安全.
4)我找不到batchOperation方法的文档,并想知道它是否已从更新版本的AFNetworking中删除 – 如果是这样 – 也许它是错误的或不好的API?为了清晰起见,我很想在循环中创建自己的操作;然后做一些状态管理来检查批处理的状态并适当地处理它.
5)你说一个图像总是被遗忘……它是稳定的 – 总是第一个还是最后一个?它在SIM上与在单元网络上的行为方式相同还是模拟的慢/不可靠连接?