I coded the following, but I am not sure if it's working the way it's supposed to:
func loadAssets(assets: [String: [String]]) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0) {
let group = dispatch_group_create()
for (assetType, ids) in assets {
switch(assetType) {
case Settings.imageAssetType:
for id in ids {
dispatch_group_enter(group)
Assets.fetchImage(id) { _ in
dispatch_group_leave(group)
}
}
case Settings.soundAssetType:
for id in ids {
dispatch_group_enter(group)
Assets.fetchSound(id) { _ in
dispatch_group_leave(group)
}
}
case Settings.jsonAssetType:
for id in ids {
dispatch_group_enter(group)
Assets.fetchJSON(id) { _ in
dispatch_group_leave(group)
}
}
case default:
for id in ids {
dispatch_group_enter(group)
Assets.fetchClip(id) { _ in
dispatch_group_leave(group)
}
}
}
}
dispatch_group_notify(group, main, self.assetsDidLoad)
}
}
Basically, a method that loads all a bunch of assets using a Dictionary with key values containing the asset type in its keys and a list of asset IDs as the values. Then I am iterating through all these asset ids for each type of asset and calling a method to fetch the asset. (I am not sure that the fetch methods are fully running asynchronously, they might have part of their code synchronously too, these methods will load the assets).
Anyway, what I am trying to do is call the method assetsDidLoad() when all of these fetch methods have finished and therefore, all the assets have been loaded, that's why I am trying to use the dispatch group functionality. However, I am pretty new to this dispatch code and I am not sure if what I am doing right now will work for my purposes.
I would appreciate it if someone could point out if this is ok or maybe to the way I should be approaching this. I would even appreciate a response in Objective-C.
Thanks!