title: Waiting inside loop for the method to finish executing
body: <p>I have a For loop in which I call a method to upload images to the server , the problem that i am facing that at a certain limit the server will force stop the opened socket so i have to upload every image at once</p>
  
  <pre><code>for (int i = 0; i &lt; paths.size(); i++) {
      transferData(paths.get(i), i);
  }
  </code></pre>
  
  <p>and the transferData Function I am using the transfer Utility aws s3 function</p>
  
  <pre><code>TransferUtility transferUtility =
              TransferUtility.builder()
                      .context(this)
                      .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
                      .s3Client(amazonS3Client)
                      .defaultBucket("name")
                      .build();
  
  TransferObserver uploadObserver = transferUtility.upload("name", name, new File(path), CannedAccessControlList.PublicRead);
      uploadObserver.setTransferListener(new TransferListener() {
  
          @Override
          public void onStateChanged(int id, TransferState state) {
              Log.d(TAG, "onStateChanged: " + id + ", " + state);
              if (TransferState.COMPLETED == state) {
              }
          }
  
          @Override
          public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
              float percentDonef = ((float) bytesCurrent / (float) bytesTotal) * 100;
              int percentage = (int) percentDonef;            
              Log.d(TAG, "onProgressChanged: " + percentage);                        
          }
  
          @Override
          public void onError(int id, Exception ex) {
              Log.e(TAG, "Error during upload: " + id, ex);
              try {
                  showToast(ex.getMessage());
              } catch (Exception e) {
                  e.printStackTrace();
              }
              ex.printStackTrace();
          }
  
      });
  </code></pre>
  
  <p>How to wait for the method to finish execution then continue the loop</p>