I'm trying to create a completion block that can execute another function after its completed, in this case it's a tableview reload. I get the error :
'async' produces '()', not the expected contextual result type 'Bool'
This is the function:
func appendAllData (completion: () -> Bool) {
if self.movieDetailsData?.poster != nil {
if let posterImage = self.movieDetailsData?.poster {
self.posterArray.append(posterImage)
}
}
if self.movieDetailsData?.overview != nil {
if let overview = self.movieDetailsData?.overview {
self.overviewArray.append(overview)
}
}
if self.movieDetailsData?.releaseData != nil {
if let releaseDate = self.movieDetailsData?.releaseData {
self.releaseInfoArray.append(releaseDate)
}
}
if self.movieDetailsData?.runtime != nil {
if let runtime = self.movieDetailsData?.runtime {
self.releaseInfoArray.append(String(describing: runtime))
}
}
if self.movieDetailsData?.genre != nil {
if let genre = self.movieDetailsData?.genre {
if genre.isEmpty {
} else {
self.releaseInfoArray.append(genre[0].name)
}
}
}
if self.movieDetailsData?.budget != nil {
if let budget = self.movieDetailsData?.budget {
self.boxOfficeArray.append(budget)
}
}
if self.movieDetailsData?.revenue != nil {
if let revenue = self.movieDetailsData?.revenue {
self.boxOfficeArray.append(revenue)
}
}
if self.movieDetailsData?.homepage != nil {
if let homepage = self.movieDetailsData?.homepage {
self.homePageArray.append(homepage)
}
}
if self.movieDetailsData?.images != nil {
if let images = self.movieDetailsData?.images {
let posters = images.backdropImages
for poster in posters {
self.imageArray.append(poster.filePath)
}
}
}
}
This is how it's used:
self.appendAllData(completion: { _ in
DispatchQueue.main.async { //error here: 'async' produces '()', not the expected contextual result type 'Bool'
self.detailTableView.reloadData()
}
})
Your completion closure signature is completion: () -> Bool
but you used () -> ()
. Just change function parameter from completion: () -> Bool
to completion: () -> Void
or completion: () -> ()
.
And you should follow njzk2's comment.