Pit*_*Pan 3 arrays reference retain ios swift
我尝试实现 Coordinator 模式的一些变体,但我在关闭时遇到了保留循环的问题。它看起来像这样:
func goTo() {
let coord = SecondViewCoordinator(nav: navigationController)
add(coord)
coord.start()
coord.deinitIfNeeded = { [weak self] in
guard let self = self else { return }
self.free(coord)
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我设置deinitIfNeeded然后,如果在SecondViewCoordinator调用中deinitIfNeeded?()控制器正确弹出,但SecondViewCoordinator即使childCoordinators数组为空,对is 的引用仍然存在。
我的 Coordinator 类看起来像这样:
func goTo() {
let coord = SecondViewCoordinator(nav: navigationController)
add(coord)
coord.start()
coord.deinitIfNeeded = { [weak self] in
guard let self = self else { return }
self.free(coord)
}
}
Run Code Online (Sandbox Code Playgroud)
内存图显示:
有任何想法吗?
In
coord.deinitIfNeeded = { [weak self] in
guard let self = self else { return }
self.free(coord)
}
Run Code Online (Sandbox Code Playgroud)
You are holding a strong reference to coord inside the closure. Try something like this;
coord.deinitIfNeeded = { [weak self, weak coord] in
guard let self = self, let coord = coord else { return }
self.free(coord)
}
Run Code Online (Sandbox Code Playgroud)
The memory graph is giving a hint that this is the case (the right side says the strong reference is in a closure).
You could also set coord.deinitIfNeeded to nil inside the closure.
| 归档时间: |
|
| 查看次数: |
162 次 |
| 最近记录: |