我的项目有mainfeed tableviewcontroller,如果我选择任何行,它将打开另一个具有不同行数的tableviewcontroller。在secondtableviewcontroller中,计时器1.0秒后,我调用chain beginupdates和endupdates,如果我保持在secondtableviewvc中,一切正常,更新将成功结束。
但如果计时器结束后,更新链开始(需要一段时间才能完成),如果用户在链尚未完成时按导航键返回mainfeed tableviewcontroller,然后链开始/结束更新从secondvc结束(当它已经关闭,而我目前在mainvc中)。ash with error更新后的行数应等于更新前的行数。但我甚至没有删除secondvc中的任何行,我使用它来更新wkwebview中单元格的高度。
所以在这种情况下,逻辑应该以secondvc结束,在我关闭它之后停止工作,并影响到我的firstvc并使其崩溃。
当用户返回时,我试图使计时器无效,但它仅在begin/end更新尚未启动时才起作用。
我试着找出我错过了什么,做错了什么。你知道我怎么打破这条链子吗?
我猜测什么开始/结束更新在另一个线程中工作,但我不直接在另一个线程中调用它。
目前,我有一个想法,如何修复它不允许用户回去,如果更新开始,还没有完成,但我不觉得这是一个好的解决方案。
当wkwebview在cell中完成时
self.timer = Timer.scheduledTimer(timeInterval: 1.0,
target: self,
selector: #selector(self.updateHeightCell),
userInfo: nil,
repeats: false)
然后我从委托rootviewcontroller调用webdidloadwithcontentheight作为secondvc。
@objc private func updateHeightCell() {
self.height = self.webView.scrollView.contentSize.height * 1.2
self.rootViewController.webDidLoadWithContentHeight(self.height, currentWebScrollPosition: scrollPositionBeforeLoadFinish)
self.webView.scrollView.isScrollEnabled = false
}
func webDidLoadWithContentHeight(_ height: CGFloat, currentWebScrollPosition: CGFloat) {
webCellIsLoaded = true
self.tableView.beginUpdates()
self.webContentHeight = height
self.tableView.endUpdates()
self.webCellReloadedCount += 1
self.tableView.contentOffset.y = currentWebScrollPosition
self.setupMaterialBarAlphaIfNecessary()
self.tableView.isScrollEnabled = true
}
后退按钮
override func didMaterialBackButtonTouch() {
// stop webView
let cells = tableView.visibleCells
for cell in cells {
if (cell.isKind(of: MaterialViewCell3.self)) {
let webCell = cell as! MaterialViewCell3
webCell.webView.stopLoading()
webCell.timer?.invalidate()
}
}
self.viewModel.avoidViewController()
guard let source = self.source else {
fatalError("\n[\(self) didMaterialBackButtonTouch] no source view controller!\n")
}
switch source {
case .myFeed:
self.performSegue(withIdentifier: String.unwindFromMaterialToMyFeedSegueID, sender: self)
case .mainFeed:
self.performSegue(withIdentifier: String.unwindFromMaterialToMainFeedSegueID, sender: self)
}
}
预期的结果是,当用户返回时,在secondvc中调用的开始/结束更新不会影响mainvc。