问题描述
我有一个问题,由于内存泄漏,在用于使用 RxDataSource 的 CollectionView 的 dataSource 函数中 [uNowned self] 更改为 [weak self]。我现在收到了返回一个没有重用标识符的空白 collectionViewCell 的崩溃。我知道我需要返回一个带有重用 ID 的单元格。
建议进行哪些更改以正确处理此问题?
有人建议在 viewDidLoad() 中设置 collectionView.dataSource = nil 会解决这个问题...
我在想,而不是在“守卫”检查中返回 CanvasItemCollectionViewCell(), 我返回 collectionView.dequeueReusableCell(for: indexPath,cellType: CanvasItemCollectionViewCell.self),但是如果 self = self 失败了,那是不是意味着 collectionView 是垃圾?
这是一个很难调试的问题,因为这种崩溃不会一直发生。
这里有一些截图来描绘我正在看的东西。
RxDataSource 代码:
func dataSource()
-> RxCollectionViewSectionedAnimatedDataSource<CanvasSectionModel> {
RxCollectionViewSectionedAnimatedDataSource<CanvasSectionModel>(
animationConfiguration: AnimationConfiguration(
insertAnimation: .fade,reloadAnimation: .fade,deleteAnimation: .fade
),configureCell: { [weak self] dataSource,collectionView,indexPath,_ in
guard let self = self else { return CanvasItemCollectionViewCell() }
switch dataSource[indexPath] {
case let .CellModel(model):
let cell = collectionView
.dequeueReusableCell(
for: indexPath,cellType: CanvasItemCollectionViewCell.self
)
cell.model = model
cell.onDeleteHandler = { _ in
self.presentDeleteConfirmation { deleteConfirmed in
guard deleteConfirmed else { return }
self.viewmodel.inputs.deletePage(withProofID: model.id)
}
}
return cell
}
}
崩溃:
解决方法
最终,问题出在这里:
cell.onDeleteHandler = { _ in
self.presentDeleteConfirmation { deleteConfirmed in
guard deleteConfirmed else { return }
self.viewModel.inputs.deletePage(withProofID: model.id)
}
}
不要使用 self
并且你不会有自我引用的问题,所以你不需要导入弱自我然后担心守卫让自我......
- 对于第一个
self
引用,将其替换为UIViewController.top()
(有关实现,请参见下文。) - 对于第二个
self
引用,改为捕获viewModel
。
extension UIViewController {
static func top() -> UIViewController {
guard let rootViewController = UIApplication.shared.delegate?.window??.rootViewController else { fatalError("No view controller present in app?") }
var result = rootViewController
while let vc = result.presentedViewController,!vc.isBeingDismissed {
result = vc
}
return result
}
}