Skip to content

Instantly share code, notes, and snippets.

@jamesrochabrun
Last active June 17, 2021 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesrochabrun/0e03eb9a9bbd21b3a3ba887841ee5584 to your computer and use it in GitHub Desktop.
Save jamesrochabrun/0e03eb9a9bbd21b3a3ba887841ee5584 to your computer and use it in GitHub Desktop.
A sample function that uses DispatchGroup API
func dispatchGroups(
from categoryIdentifiers: [ItunesCategoryIdentifier]) {
// 1
let dispatchGroup = DispatchGroup()
// 2
var sections: [ItunesCategorySection] = []
// 3
for categoryIdentifier in categoryIdentifiers {
// 4
dispatchGroup.enter()
// 5
service.fetch(Feed<ItunesResources<FeedItem>>.self, itunes: Itunes(mediaTypePath: categoryIdentifier.mediaType)).sink { _ in
dispatchGroup.leave() // 6
} receiveValue: { feed in
// 7
let feedItemViewModels = feed.feed?.results.compactMap { FeedItemViewModel(model: $0) } ?? []
sections.append(ItunesCategorySection(sectionID: categoryIdentifier, cellIDs: feedItemViewModels))
}.store(in: &cancellables)
}
// 8
dispatchGroup.notify(queue: .main) {
self.itunesSections = sections.sorted { $0.sectionID.rawValue < $1.sectionID.rawValue }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment