ios – NSOperation Queue行为异常

我的任务是逐个将多个图像上传到服务器.所以我正在使用批量操作流程.每次我开始上传程序时,一些操作特别是第一个操作在它启动时就完成并且图像没有上传,然后批量上载过程继续正常,并且遗漏了其他图像的罕见故障.

我使用的代码如下: –

-(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上与在单元网络上的行为方式相同还是模拟的慢/不可靠连接?

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...