简体   繁体   中英

Escaping closure captures non-escaping parameter 'completion' (Swift 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

使用@escaping

private func getTracks(completion:@escaping () -> ())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM