Last active
June 17, 2021 21:30
-
-
Save jamesrochabrun/0e03eb9a9bbd21b3a3ba887841ee5584 to your computer and use it in GitHub Desktop.
A sample function that uses DispatchGroup API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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