5

In my project, I came across a situation when I need to use the background queue to create an AVPlayerItem (which I create in setupTrackModels function). I'd like do it in getTracks function, and this method must also have a completion handler which I need to call in main thread, but I cannot make them friends in any way. I get compiler error: Escaping closure captures non-escaping parameter 'completion' Maybe someone can tell me how to do this or show another way.

I'd like to do something like this:

var content: [URL] = []
var tracks: [TrackModelProtocol] = []

private func getTracks(completion: () -> ()) {
    DispatchQueue.global(qos: .background).async { //Error: Escaping closure captures non-escaping parameter 'completion'
        self.tracks = self.setupTrackModels(content: self.content)
        
        DispatchQueue.main.async { //Error: Escaping closure captures non-escaping parameter 'completion'
            completion()
        }
    }
}

And then I'd like to use the function like this:

getTracks {
   tableView.reloadData()
   //or something else
}

I don't want to use tableView.reloadData() in the DispatchQueue.main. block because I call getTracks several times and I want to implement different logic in it's completion block

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • 2
    You need to mark the `completion` parameter as `@escaping` – Paulw11 Oct 22 '20 at 12:11
  • FWIW, as a matter of convention, we now generally use `Void` rather than `()` for the closures return type, e.g. `func getTracks(completion: @escaping () -> Void) { ... }`. – Rob Oct 22 '20 at 22:45

1 Answers1

14

Use @escaping

private func getTracks(completion:@escaping () -> ())
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87