Based on @HangarRash's recommendations, I've added the following changes:
DispatchQueue.global().async {
do { try fileManager.unzipItem(at: sourceURL, to: destinationURL, progress: unzipProgress) }
catch { print("error") }
}
Full code:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let url = URL(string: "1.zip")!
let downloadManager = DownloadManager()
downloadManager.identifier = indexPath.row+1
downloadManager.collectionId = 0
downloadManager.folderPath = "\(indexPath.row+1)"
let downloadTaskLocal = downloadManager.activate().downloadTask(with: url)
downloadTaskLocal.resume()
downloadManager.onSuccess = { [weak self] row in
guard let self = self else { return }
DispatchQueue.main.async {
self.items[row - 1].state = .completed
self.reloadItem(indexPath: .init(row: row - 1, section: 0))
if fileManager.fileExists(atPath: destination.path){
let sourceURL = URL(fileURLWithPath: "\(destination.path)/1.zip")
let destinationURL = URL(fileURLWithPath: destination.path)
let unzipProgress = Progress()
let observation = unzipProgress.observe(\.fractionCompleted) { progress, _ in
print("Extraction progress: ", progress.fractionCompleted)
if progress.fractionCompleted == 1.0 {
self.items[row - 1].state = .unzipped
self.reloadItem(indexPath: .init(row: row - 1, section: 0))
}
}
DispatchQueue.global().async {
do { try fileManager.unzipItem(at: sourceURL, to: destinationURL, progress: unzipProgress) }
catch { print("error") }
}
observation.invalidate()
} else { print("error") }
}
}
}