为什么我从将弱自我传递到静态函数的闭包中得到一个保留周期?
Why am I getting a retain cycle from passing weak self in to a static function's closure?
我的 AchievementViewController
不会从内存中释放,除非我注释掉下面的函数
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)
}
}
self到底被保留在什么地方来创建一个循环?它作为 weak
引用传递给 NetworkConnection 闭包,当此闭包传递给 DispatchQueue
时,我希望它在延迟结束后释放。
试着评论这一行
self?.configureCollectionView()
mb 是问题所在,因为 [weak self]
足以修复此闭包中的保留
我的 AchievementViewController
不会从内存中释放,除非我注释掉下面的函数
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)
}
}
self到底被保留在什么地方来创建一个循环?它作为 weak
引用传递给 NetworkConnection 闭包,当此闭包传递给 DispatchQueue
时,我希望它在延迟结束后释放。
试着评论这一行
self?.configureCollectionView()
mb 是问题所在,因为 [weak self]
足以修复此闭包中的保留