I am trying to setup the following method to perform the success block once all the show.getVideosForShow() success blocks have been performed and all videos appended. Note: show.getVideosForShow() is asynchronous and could take a few seconds to get a result. Could someone please provide some assistance?
private func getNextVideoRecommendations(success: ([Video]) -> ()) {
var relatedVideos = [Video]()
if let relatedShows = self.videoCurrentlyPlaying?.show?.getShowsWithSameGenre(fetchLimit: 3) {
for show in relatedShows {
show.getVideosForShow(tvSeason: nil, longForm: true, sortType: VideoSort.Latest, success: { (videos: [Video]) in
print("Found Related Show: \(show.title)")
if videos.count > 0 {
relatedVideos.append(videos[0])
}
})
}
print("Finished all operations")
success(relatedVideos)
}
}
completionhandlerforgetNextVideoRecommendationsmethod and call success(relatedvideos) from where you are callgetNextVideoRecommendationsfor (index, show) in relatedShows {& check for conditionif index == relatedShows.count-1at the last of for loop to callsuccess(relatedVideos)