Completion block never gets called?

We have a function that when it finishes another function should be called in it's completion block but whatever is inside the completion block is never called. Here is the function:

func appendAllData (completion: () -> Void) {

    guard let movieDetails = self.movieDetailsData else {
      // handle nil case
      return;
    }

    if let posterImage = self.movieDetailsData?.poster {
      self.posterArray.append(posterImage)
    }

    if let overview = self.movieDetailsData?.overview {
      self.overviewArray.append(overview)
    }

    if let releaseDate = self.movieDetailsData?.releaseData {
      self.releaseInfoArray.append(releaseDate)
    }

    if let runtime = self.movieDetailsData?.runtime {
      self.releaseInfoArray.append(String(describing: runtime))
    }

    if let genre = self.movieDetailsData?.genre {
      if !genre.isEmpty {
        self.releaseInfoArray.append(genre[0].name)
      }
    }

    if let budget = self.movieDetailsData?.budget {
      self.boxOfficeArray.append(budget)
    }

    if let revenue = self.movieDetailsData?.revenue {
      self.boxOfficeArray.append(revenue)
    }

    if let homepage = self.movieDetailsData?.homepage {
      self.homePageArray.append(homepage)
    }

    if let images = self.movieDetailsData?.images {
      self.imageArray += images.backdropImages.map{ $0.filePath }
    }
  }

Here is how it's used:

self.appendAllData(completion: { _ in
//Nothing inside here gets called
  DispatchQueue.main.async {

    print(self.movieDetailsData?.poster )

    if let bgImage = self.movieDetailsData?.poster {
      self.backgroundImage.sd_setImage(with: URL(string:"\(baseImageURL)\(bgImage)"))
      print("background pic loaded")
      self.backgroundImage.addBlurEffect()
    }

    self.detailTableView.reloadData()
  }
})

Nothing inside the completion block is called, any idea how to fix this?


ANSWERS:


I believe you need to call the completion() at the end for it execute your completion code.

func appendAllData (completion: () -> Void) {

    guard let movieDetails = self.movieDetailsData else {
      // handle nil case
      return;
    }

    if let posterImage = self.movieDetailsData?.poster {
      self.posterArray.append(posterImage)
    }

    if let overview = self.movieDetailsData?.overview {
      self.overviewArray.append(overview)
    }

    if let releaseDate = self.movieDetailsData?.releaseData {
      self.releaseInfoArray.append(releaseDate)
    }

    if let runtime = self.movieDetailsData?.runtime {
      self.releaseInfoArray.append(String(describing: runtime))
    }

    if let genre = self.movieDetailsData?.genre {
      if !genre.isEmpty {
        self.releaseInfoArray.append(genre[0].name)
      }
    }

    if let budget = self.movieDetailsData?.budget {
      self.boxOfficeArray.append(budget)
    }

    if let revenue = self.movieDetailsData?.revenue {
      self.boxOfficeArray.append(revenue)
    }

    if let homepage = self.movieDetailsData?.homepage {
      self.homePageArray.append(homepage)
    }

    if let images = self.movieDetailsData?.images {
      self.imageArray += images.backdropImages.map{ $0.filePath }
    }

    completion()
  }


 MORE:


 ? How to get ref url from PHPhotoLibrary in ioS?
 ? Best Way utilize PHPhotoLibrary for display Camera Roll image in UICollectionView with custom Cell
 ? Saving a photo in a custom album fails when run for the first time
 ? Show Photos in custom album but not in Camera Role/My Photo Stream
 ? How does the maxConcurrentOperationCount of the NSURLSession delegateQueue effects task concurency
 ? ios URLSession background download FIFO mechanism
 ? ios URLSession background download FIFO mechanism
 ? ios URLSession background download FIFO mechanism
 ? NSURLSession in background, posting and receiving response from server?
 ? Most appropiate way to continue an NSURLConnection request in background