我目前正在尝试根据用户的游戏要求下载多个文件。我的目标是让它一次仅下载一个文件,并停止执行代码,直到该文件完成下载为止。
我的代码是一个JSON对象数组,每个对象都包含一个将它们下载到的路径以及该文件的URL。我正在遍历数组,并使用AlamoFire封闭地下载它们.downloadProgress。
Alamofire.download(
json["url"].stringValue,
method: .get,
to: destination).downloadProgress(closure: { (Alamoprogress) in
info.stringValue = "Downloading: " + filename
progress.doubleValue = Alamoprogress.fractionCompleted * 100
}).response(completionHandler: { (DefaultDownloadResponse) in
})
Run Code Online (Sandbox Code Playgroud)
为了确保每次仅下载一个文件,我设置了DispatchQueue并将Alamofire请求移至同步操作中:
for jsonObject in jsonArray{
queue.async {
print("Started downloading " + filename)
info.stringValue = "Downloading: " + filename
info.placeholderString = "Downloading: " + filename
info.isEnabled = true
info.isHidden = false
progress.isHidden = false
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = filepath
return (documentsURL, [.removePreviousFile])
}
Alamofire.download(
json["url"].stringValue, …Run Code Online (Sandbox Code Playgroud)