Swift:当调用者 viewController 为 deinit() 时,DispatchQueue.async 调用会发生什么
我正在开发一个 ViewController,我在其中调用了 viewModel 来执行 DispatchQueue.async 调用。在异步任务开始之后和结束之前,我的任务 ViewController 通过按后退按钮来定义。在这种情况下,我的异步任务会发生什么?它会保留线程或内存块或导致内存泄漏吗?如果发生这种情况,有什么办法可以取消我的异步任务吗?
为了理解,我添加了演示类:
Class A: ViewController {
let viewModel = B()
func callViewModelAsyncFunction() {
viewModel.viewModelAsyncFunction()
}
}
Class B {
private let sessionQueue = DispatchQueue(label: "Session.Queue")
private let sessionGroup = DispatchGroup()
func viewModelAsyncFunction() {
sessionQueue.async {
self.sessionGroup.wait()
self.sessionGroup.enter()
//do my other task signal producer call {
//}
}
}
}
I am working on a ViewController where I called my viewModel to do a DispatchQueue.async call. After the async task starts and before the end my task ViewController is deinited by pressing the back button. In that case, what will be happened to my async task? Is it gonna hold a thread or a memory block or cause a memory leak? If that happens, is there any way to cancel my async task?
For understanding, I am adding demo classes:
Class A: ViewController {
let viewModel = B()
func callViewModelAsyncFunction() {
viewModel.viewModelAsyncFunction()
}
}
Class B {
private let sessionQueue = DispatchQueue(label: "Session.Queue")
private let sessionGroup = DispatchGroup()
func viewModelAsyncFunction() {
sessionQueue.async {
self.sessionGroup.wait()
self.sessionGroup.enter()
//do my other task signal producer call {
//}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于没有人回答我天真的问题,我做了一些实验,这是我在 Playgrounds 上的实验代码
,实验结果是
从我的实验中,我了解到
尽管调用者类已定义,但已完成。
Since no one goona respond to my naive question, I do some experiments, and here is my experiment code on Playgrounds
And experiment result is
From my experiment, I understand that
completed although the caller class is deinited.