私はAlamofireリクエストを関数の一部として持っており、残りの関数が実行される前に完了するようにリクエストが必要です。あなたは以下の完全な機能を見ることができます。Alamofireリクエストが完了するのを待つ必要があります - スウィフト
@IBAction func btnScanPressed(_ sender: Any) {
// Retrieve the QRCode content
// By using the delegate pattern
readerVC.delegate = self
readerVC.completionBlock = { (result: QRCodeReaderResult?) in
print(result?.value)
Alamofire.request((result?.value)!).responseString { response in
debugPrint(response)
if let json = response.result.value {
let returnedJSON = JSON.init(parseJSON: json);
//TESTED -> this section does work and does update the values
self.co.setVideoURL(url: (returnedJSON["VideoURL"].string)!)
self.co.setDisplayText(text: returnedJSON["DisplayText"].string!)
self.co.setHasUpdated(value: true)
print("VARIABLES NOW SET")
}
}
}
// Presents the readerVC as modal form sheet (This needs to happen AFTER the Alamofire has completed)
readerVC.modalPresentationStyle = .formSheet
present(readerVC, animated: true, completion: nil)
}
回答を解析してオブジェクトに値を割り当てるためにswiftyJSONを使用しています。関数全体が完了する前にAlamofireリクエストを完了する必要があります。どんな提案も大歓迎です!
モーダルのプレゼンテーションコードをネットワーク要求完了ブロックに移動するだけでいいですか(また、おそらくメインスレッド上で実行されるようにする必要があります)。完了ブロック内のコードは、要求が完了した後に発生します。 –