7

私は、サーバーに複数のイメージを1つずつアップロードする作業があります。だから私はこれのためのバッチ操作プロセスを使用しています。私がアップロード手順を開始するたびに、特に最初の操作が始まるとすぐに完了し、画像がアップロードされず、バッチアップロード処理がうまくいっていて、他の画像が欠落しています。次のように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のうち、アップロード取得された画像のアップロードを開始実行なっています。 1つの画像は常に除外されます。また、私がグリッド内にImageだけを持っていれば、それは正常にアップロードされます。

ありがとうございました。

+0

誰にも実行可能な解決策がありますか? –

+0

albumImagesCollectionから削除された画像は、アップロードプロセスから除外されていますか?また、最初に残されたイメージや最後のイメージは残っていますか? – user2695712

+0

はいそれは除外され、そのイメージの完了ブロックが実行されます。これは実際にコレクションビューから削除します。これは、ほとんどの場合、95%の症例で2人の最初の画像であり、4人以上の場合は2画像である。 Hevあなたはまだ問題を特定しましたか? –

答えて

2

いくつかの考え...

1)進捗状況更新ブロック。これは、どの操作が完了したかはわかりません。それらの数だけしかないので、コードが常にそうであるとコードが完了していない可能性があります。

2)完了ブロックは一連の操作を取ります。これがすべての操作で一度呼び出されるのか、完了した操作の配列で複数回呼び出されるのか不思議です。あなたは見るために配列の長さを見ることができます。操作のサブセットが呼び出される場合は、すでにアップロードされているアセットを削除してクリーンアップ作業を行う場所になります。これは、どの操作が完了したかを知っているためです。

3)操作をキューに追加する前にアップロードの進行ブロックを設定します。安全のために。

4)batchOperationメソッドのドキュメントが見つかりませんでした。また、最新のバージョンのAFNetworkingから削除されているのだろうか?そうであれば、バグか良いAPIではないのだろうか?明快にするために私自身の操作をループで作成したいと思うでしょう。バッチの状態をチェックして適切に処理するための小さな状態管理を行います。

5)あなたは1つのイメージが常に残っていると言います....それは安定していますか?いつも最初か最後か?それは、セルネットワーク上のsim vsやシミュレートされた遅い/信頼できない接続で同じように動作しますか?

+0

1.完了した操作を通知します。 - currentUploadIndexは変数です。 2.完了ブロックは、すべての操作が完了したときにのみ1回だけ実行されます。 3.あなたが提案したコードの進捗ブロックはどこに置いておきますか? 4.あなたのアプローチのための少しのコードを表示していただけますか? 5.それは非常にランダムですが、どのような種類のセルラネットワークでも同じように動作します。このすべてから抜け出す方法を提案してください。 –

関連する問題