[英]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).在我的项目中,我遇到了需要使用后台队列来创建 AVPlayerItem(我在
setupTrackModels
函数中创建)的情况。 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.我想在
getTracks
函数中执行此操作,并且此方法还必须有一个我需要在主线程中调用的完成处理程序,但我无法以任何方式使它们成为朋友。 I get compiler error: Escaping closure captures non-escaping parameter 'completion'
Maybe someone can tell me how to do this or show another way.我收到编译器错误:
Escaping closure captures non-escaping parameter 'completion'
也许有人可以告诉我如何做到这一点或展示另一种方式。
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.我不想在 DispatchQueue.main 中使用
tableView.reloadData()
。 block because I call getTracks
several times and I want to implement different logic in it's completion block块,因为我多次调用
getTracks
并且我想在它的完成块中实现不同的逻辑
使用@escaping
private func getTracks(completion:@escaping () -> ())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.