Use DispatchGroup for download licences

This commit is contained in:
Florian Gabach
2018-02-26 14:22:37 +01:00
parent a7a6a1b315
commit 318d3e1f8e
2 changed files with 9 additions and 11 deletions
+9 -11
View File
@@ -16,22 +16,20 @@ class LicenceDownloader: NSObject {
class func downloadLicences(licences: [LicenceFile],
config: OpenSourceControllerConfig,
completion: @escaping () -> Void) {
// Number of licences
let licenceCount = licences.count
// Already licence downloaded
var alreadyDownloaded = 0
let licenceGroupe = DispatchGroup()
// Download licence's detail for every lience
for licence in licences {
licenceGroupe.enter()
licence.downloadLicenceDetail(config: config, completion: { () in
alreadyDownloaded += 1
// If this is the last licence, perfom completion
if licenceCount == alreadyDownloaded {
completion()
}
licenceGroupe.leave()
})
}
// When everything is downloaded
licenceGroupe.notify(queue: .main) {
completion()
}
}
}