IOS開発中の非同期ネットワーク要求に同期ロジックを実現する。


IOS開発中の非同期ネットワーク要求に同期ロジックを実現する。
前提:
いくつかの問題があるかもしれません。例えば、複数のデータをアップロードする場合、複数のデータをアップロードして成功したら、一定の処理をします。そして、一つ一つアップロードします。もしどのアップロードが失敗したら、後はアップロードしなくてもいいです。直接にエラーを報告します。
以前のASIのネットワークライブラリには同期要求のインターフェースがありましたので、AFNetworkのネットワークライブラリは非同期のネットワーク要求しかありませんでした。どうやって実現しますか?
1.循環非同期グループ

- (void)uploadFile:(NSArray *)imageArray atIndex:(NSInteger)index imagesCount:(NSInteger)count completeBlock:(uploadCompleteBlock)block {
 FNCircleImage *aTCImage = imageArray[index];
 NSString *filepath = aTCImage.localFilePath;
 [self.resourceManager upload:filepath progress:nil completion:^(NSString * _Nullable urlString, NSError * _Nullable error) {
  if (error == nil) {
   aTCImage.remoteUrl = urlString;

   NSInteger idx = index + 1;
   if (idx >= count) {
    block(nil);
   } else {
    [self uploadFile:imageArray atIndex:idx imagesCount:count completeBlock:block];
   }
  } else {
   block(error);
  }
 }];
}

2.信号量非同期

__block NSError *e = nil;
[imageArray enumerateObjectsUsingBlock:^(NSString *filePath, NSUInteger idx, BOOL * _Nonnull stop) {
 __block dispatch_semaphore_t t = dispatch_semaphore_create(0);
 [self upload:filepath progress:nil completion:^(NSString * _Nullable urlString, NSError * _Nullable error) {
  if (error == nil) {
   
  } else {
   e = error;
   *stop = YES;
  }
  dispatch_semaphore_signal(t);
 }];
 dispatch_semaphore_wait(t, DISPATCH_TIME_FOREVER);
}];
3.NSOperation Queue制御行列
1)NSOperationを継承してアップロードロジックを実現し、通知またはブロックコールの発行を完了する。
2)アップロードデータでOperation配列を作成し、NSOperation Queに追加して実行する。
3)コールバックを完了した結果と個数によって判断した結果、途中に失敗があれば、未実行のOperationをオフにすることができます。
読んでくれてありがとうございます。みなさんのご協力をお願いします。ありがとうございます。