0
func credentials() -> AWSTask<AWSCredentials> {
var task:AWSTask<AWSCredentials>
// let task=AWSTask<AWSCredentials>(result:nil)
print("hello")
let svc=ServerConnection(action: "m_get_token")
let req=svc.createRequestWithoutBody("POST")
let queue=DispatchQueue(label: "credentialsqueue")
task=AWSTask<AWSCredentials>(result:nil)
svc.getResponse(req){
(appresp)->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<AWSCredentials>(result:self.cred!)
return task
}
在上面的代碼我在getResponse
功能....副螺紋設置self.cred
這需要時間,因爲它是一個服務器呼叫.... self.cred
在主線程是返回nil因爲它正在執行的時間早於我們在輔助線程中設置...如何返回來自輔助線程的值self.cred
(不要停止主線程)用戶不必經歷延遲...但不應該返回零(應該返回輔助線程中的值)。多線程在迅速
您可能需要使用/的closures__而不是返回一個值__completion塊,有許多在線的例子可以告訴你這個概念的正確方法。 – holex