Swift 为什么我会得到一个从弱自我传递到静态函数的保留周期';结束?
除非我注释掉下面的函数,否则我的Swift 为什么我会得到一个从弱自我传递到静态函数的保留周期';结束?,swift,memory-leaks,automatic-ref-counting,Swift,Memory Leaks,Automatic Ref Counting,除非我注释掉下面的函数,否则我的实现viewcontroller不会从内存中释放 NetworkConnection.achievementList(for: -1) { [weak self] response in guard let sections = response.object as? [AchievementListSection] else { return print("Network failure")
实现viewcontroller
不会从内存中释放
NetworkConnection.achievementList(for: -1) { [weak self] response in
guard let sections = response.object as? [AchievementListSection] else {
return print("Network failure")
}
self?.sections = sections
self?.configureCollectionView()
}
这个函数的定义如下,目前我们只使用延迟异步调用发送存根响应
static func achievementList(for identifier: Int64, responseHandler: RequestResponseClosure?) {
let stubResponse = ResponseObject(object: AchievementListSection.exampleList as NSArray, code: .success)
let randomDelayMilliseconds = Int(arc4random_uniform(1000))
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(randomDelayMilliseconds)) {
responseHandler?(stubResponse)
}
}
在这里,自我到底在哪里被保留来创造一个循环?它作为弱
引用传递给NetworkConnection闭包,反过来,当此闭包传递给DispatchQueue
时,我希望它在延迟过后释放。尝试注释这一行
self?.configureCollectionView()
mb这就是问题所在,因为
[弱自我]
对于这个闭包中的fix retain已经足够了在闭包中捕获弱自我
的事实应该可以防止retain循环,无论实现列表
是如何实现的。你确定结束是有问题的,而问题不是从别处开始的吗?@Cristik,很不幸,我尽可能肯定。如果我注释掉该函数,viewcontroller将被释放。如果不注释它,viewcontroller将永远不会发布。请尝试注释self?.configureCollectionView()以查看mb它是problem@Kstin啊,谢谢你。我找错人了。请随便把它作为一个答案,我会把它标记为正确的。