我正在下载一个.zip文件,其中包含一个使用Alamofire纹理的3D模型。文件大小目前为20 is。使用ZIPFoundation后,解压缩文件所需的时间是50~50。文档表明,解压缩接近20 1s的文件应该使用<1s。然而,我没有取得这些成果。下面是完成文件下载请求和解压缩的代码:
func downloadRequest(url: String, method: HTTPMethod, parameters: [String:String]?, headers: [String:String]?, destFileName: String, completion: @escaping (URL?) -> Void) {
//Initial parameters
let fullDestName = destFileName + ".zip"
let fm = FileManager.default
guard let documentsDirectory = fm.urls(for: .documentDirectory, in: .userDomainMask).first else {
return
}
//Initialise temporary download location
let destination: DownloadRequest.DownloadFileDestination = { _,_ in
let temporaryDownloadDest = documentsDirectory.appendingPathComponent(fullDestName)
return (temporaryDownloadDest, [.createIntermediateDirectories,.removePreviousFile])
}
//Call API to begin download request
Alamofire.download(url, method: method, parameters: parameters, headers: headers, to: destination).response { (response) in
let responseError = response.error
if responseError == nil {
print("File successfully downloaded")
if let downloadFilePath = response.destinationURL?.path { //.....Documents/name.zip
let sourceURL = URL(fileURLWithPath: downloadFilePath)
let destinationURL = documentsDirectory.appendingPathComponent(destFileName) //....Documents/name
DispatchQueue.global().async { //Failing to do this will lock up main thread
do {
try fm.createDirectory(at: destinationURL, withIntermediateDirectories: true, attributes: nil)
try fm.unzipItem(at: sourceURL, to: destinationURL) // <------ UNZIPPING THE FILE HERE
completion(destinationURL)
} catch {
print("Extraction of ZIP archive failed with error: \(error)")
completion(nil)
}
}
} else {
print("file path was not found")
completion(nil)
}
} else {
print("There was an error: \(responseError!)")
completion(nil)
}
}
}我下载和解压缩文件的方法有什么问题吗?还是对库的限制?如果是这样的话,有人能推荐另一个加速这个过程的库吗?提前感谢
发布于 2020-08-06 05:04:20
解压缩存档的代码看起来不错。
在Swift中,优化的构建和非优化的构建有很大的性能差异。当您切换到“版本”构建时,解压缩需要多长时间?
https://stackoverflow.com/questions/63274798
复制相似问题