Rum*_*min 2 uitableview ios swift swift4.2
我们已经为我们的应用程序实现了滑动删除功能,但不知何故,我们在 Crashlytics 上看到了间歇性的产品崩溃。
我一直在关注有关此崩溃的几篇 StackOverflow 帖子,但我无法获得此崩溃的确切原因。
我曾多次尝试产生此崩溃,但每次都运行良好。
如果我在这里做错了什么,有什么想法或想法吗?以下是崩溃报告和当前可用的代码片段。
@available(iOS 11.0, *)
private func actionForType(alertID: String, swipeAction: AlertSwipeActionType, indexPath: IndexPath) -> UIContextualAction {
let contexualAction = UIContextualAction(style: .normal, title: nil) { [weak self] (action, view, completion) in
guard let strongSelf = self else {
return
}
switch swipeAction {
......
case .affirm:
completion(true)
strongSelf.dispositionAlert(id: alertID, status: true, indexPath: indexPath)
......
}
}
......
return contexualAction
}
fileprivate func dispositionAlert(id: String, status: Bool, indexPath: IndexPath) {
let dispositionRequest = AlertDispositionUpdateBody(id: id, disposition: status, questionId: nil)
self.updateAlertDispositionStatus(request: dispositionRequest) { [weak self] in
guard let strongSelf = self else {
return
}
strongSelf.removeCellWithAnimationAt(indexPath: indexPath)
strongSelf.loadAlerts()
}
}
fileprivate func removeCellWithAnimationAt(indexPath: IndexPath) {
DispatchQueue.main.async {
self.tableView.beginUpdates() // likely not required
self.removeAlertAtIndexPath(indexPath)
self.tableView.deleteRows(at: [indexPath], with: .fade)
self.tableView.endUpdates() // likely not required either
}
}
@objc func loadAlerts() {
self.startLoadingAnimation()
self.alertsFooterList.removeAll()
AlertsManager.sharedInstance.loadMemberAlerts()
}
fileprivate func removeAlertAtIndexPath(_ indexPath: IndexPath) {
let alertStatus = self.alertTabType.statusToLoad[indexPath.section]
if let alerts = self.alertsList[alertStatus],
alerts.count > indexPath.row {
self.alertsList[alertStatus]?.remove(at: indexPath.row)
}
}
Run Code Online (Sandbox Code Playgroud)
崩溃告诉您发生了什么:当您尝试删除该行时,它已经被删除了。现在这看起来很奇怪,因为当您呈现alertView 时该行就在那里。因此,在显示alertView 和删除该行之间,该行已被其他源删除。阅读你的代码,这显然是可能的。显示警报视图和删除行之间有两次延迟。第一个可能是用户确认删除之前需要很长的时间,第二个 DispatchQueue.main.async通常相当快,但仍然可能导致这些类型的错误。
不要记录要删除的indexPath(因为indexPath可能会在用户确认时更改),而是记录您要删除的项目的ID。当用户最终确认警报时,查找它在表视图中的位置,如果找到则将其删除。
更深层次的问题是,您有两个事实来源 - 表视图和数据源,但它们不同步。最好先更新数据源,然后编写自动同步表视图的代码。这样他们就始终保持同步。https://github.com/Instagram/IGListKit是一个已经实现此功能的解决方案,您可能会通过使用它获得价值。
| 归档时间: |
|
| 查看次数: |
4840 次 |
| 最近记录: |