title: Multithreading in swift
body: <pre><code>   func credentials() -&gt; AWSTask&lt;AWSCredentials&gt; {
      var task:AWSTask&lt;AWSCredentials&gt;
      // let task=AWSTask&lt;AWSCredentials&gt;(result:nil)
      print("hello")
      let svc=ServerConnection(action: "m_get_token")
      let req=svc.createRequestWithoutBody("POST")
     let queue=DispatchQueue(label: "credentialsqueue")
      task=AWSTask&lt;AWSCredentials&gt;(result:nil)
  
      svc.getResponse(req){
          (appresp)-&gt;Void in
  
  
          print("app response data xx is \(appresp)")
          let k=appresp as? NSDictionary
          let datadict=k!["m_response_data"]! as? NSDictionary
  
          let kid=datadict?["AccessKeyId"]as?String
          let skid=datadict?["SecretAccessKey"]as?String
          let exp=datadict?["Expiration"]as?String
          let stoken=datadict?["SessionToken"]as?String
  
          let dateFormatter = DateFormatter()
          dateFormatter.locale = Locale(identifier: "en_US_POSIX")
          dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
          let date = dateFormatter.date(from: exp!)
              DispatchQueue.main.async {
          self.cred=AWSCredentials(accessKey: kid!, secretKey: skid!, sessionKey: stoken!, expiration: date!)     
      }
      print("self.cred is \(self.cred!)")
      task=AWSTask&lt;AWSCredentials&gt;(result:self.cred!)
      return task
  }
  </code></pre>
  
  <p>In the above code i set up <code>self.cred</code> in the secondary thread in the <code>getResponse</code> function....which is taking time as it's a server call....<code>self.cred</code> in the main thread is returning nil as it is being executed early than before we set up in the secondary thread...how to return the value <code>self.cred</code> which is from the secondary thread.(don't stop main thread)User does not have to experience the delay...but nil should not be returned(value in the secondary thread should be returned).</p>