ios – NSOperation Queue行为异常

ios – NSOperation Queue行为异常,第1张

概述我的任务是逐个将多个图像上传到服务器.所以我正在使用批量 *** 作流程.每次我开始上传程序时,一些 *** 作特别是第一个 *** 作在它启动时就完成并且图像没有上传,然后批量上载过程继续正常,并且遗漏了其他图像的罕见故障. 我使用的代码如下: – -(void)callWSToUploadRxs{ NSLog(@"the total assets maintained are %lu", (unsigne 我的任务是逐个将多个图像上传到服务器.所以我正在使用批量 *** 作流程.每次我开始上传程序时,一些 *** 作特别是第一个 *** 作在它启动时就完成并且图像没有上传,然后批量上载过程继续正常,并且遗漏了其他图像的罕见故障.

我使用的代码如下: –

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

总结

以上是内存溢出为你收集整理的ios – NSOperation Queue行为异常全部内容,希望文章能够帮你解决ios – NSOperation Queue行为异常所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1099371.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存