From 92554faddc6534754916fbfcea2fb77cc1d19f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20B=C3=B6ttcher?= Date: Tue, 4 Jan 2022 09:03:56 +0100 Subject: [PATCH 1/2] avoiding duplicate timers and crashes in "debug" environment --- .../HomeView/HVRenderState.swift | 20 +++++++++++++------ .../HomeView/HVTableDataSource.swift | 17 ++++++++-------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Signal/src/ViewControllers/HomeView/HVRenderState.swift b/Signal/src/ViewControllers/HomeView/HVRenderState.swift index 66e63ccc19..75576c6a67 100644 --- a/Signal/src/ViewControllers/HomeView/HVRenderState.swift +++ b/Signal/src/ViewControllers/HomeView/HVRenderState.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2021 Open Whisper Systems. All rights reserved. +// Copyright (c) 2022 Open Whisper Systems. All rights reserved. // import Foundation @@ -42,27 +42,35 @@ public class HVRenderState: NSObject { } @objc - func thread(forIndexPath indexPath: IndexPath) -> TSThread? { + func thread(forIndexPath indexPath: IndexPath, failDebug: Bool = true) -> TSThread? { guard let section = HomeViewSection(rawValue: indexPath.section) else { - owsFailDebug("Invalid section: \(indexPath.section).") + if failDebug { + owsFailDebug("Invalid section: \(indexPath.section).") + } return nil } switch section { case .pinned: guard let thread = pinnedThreads[safe: indexPath.row]?.value else { - owsFailDebug("No thread for index path: \(indexPath)") + if failDebug { + owsFailDebug("No thread for index path: \(indexPath)") + } return nil } return thread case .unpinned: guard let thread = unpinnedThreads[safe: indexPath.row] else { - owsFailDebug("No thread for index path: \(indexPath)") + if failDebug { + owsFailDebug("No thread for index path: \(indexPath)") + } return nil } return thread default: - owsFailDebug("Invalid index path: \(indexPath).") + if failDebug { + owsFailDebug("Invalid index path: \(indexPath).") + } return nil } } diff --git a/Signal/src/ViewControllers/HomeView/HVTableDataSource.swift b/Signal/src/ViewControllers/HomeView/HVTableDataSource.swift index 8f4b048e89..2d39d193dd 100644 --- a/Signal/src/ViewControllers/HomeView/HVTableDataSource.swift +++ b/Signal/src/ViewControllers/HomeView/HVTableDataSource.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2021 Open Whisper Systems. All rights reserved. +// Copyright (c) 2022 Open Whisper Systems. All rights reserved. // import Foundation @@ -33,6 +33,8 @@ public class HVTableDataSource: NSObject { return } + updateTimer?.invalidate() + updateTimer = nil if let interval = nextUpdateAt?.timeIntervalSinceNow { updateTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: false) { [weak self] (_) in if let self = self { @@ -44,9 +46,6 @@ public class HVTableDataSource: NSObject { } self?.updateAndSetRefreshTimer() } - } else if updateTimer != nil { - updateTimer?.invalidate() - updateTimer = nil } } } @@ -86,15 +85,15 @@ extension HVTableDataSource { } @objc - func threadViewModel(forIndexPath indexPath: IndexPath) -> ThreadViewModel? { - guard let thread = self.thread(forIndexPath: indexPath) else { + func threadViewModel(forIndexPath indexPath: IndexPath, failDebug: Bool = true) -> ThreadViewModel? { + guard let thread = self.thread(forIndexPath: indexPath, failDebug: failDebug) else { return nil } return self.threadViewModel(forThread: thread) } - func thread(forIndexPath indexPath: IndexPath) -> TSThread? { - renderState.thread(forIndexPath: indexPath) + func thread(forIndexPath indexPath: IndexPath, failDebug: Bool = true) -> TSThread? { + renderState.thread(forIndexPath: indexPath, failDebug: failDebug) } } @@ -770,7 +769,7 @@ extension HVTableDataSource { public func updateVisibleCellContent(at indexPath: IndexPath, for tableView: UITableView) -> Bool { AssertIsOnMainThread() - if let primKey = threadViewModel(forIndexPath: indexPath)?.threadRecord.uniqueId, (tableView.indexPathsForVisibleRows ?? []).contains(indexPath) { + if let primKey = threadViewModel(forIndexPath: indexPath, failDebug: false)?.threadRecord.uniqueId, (tableView.indexPathsForVisibleRows ?? []).contains(indexPath) { for cell in tableView.visibleCells { if let homeCell = cell as? HomeViewCell, let myKey = homeCell.thread?.uniqueId, myKey == primKey, let token = buildCellConfigurationAndContentTokenSync(forIndexPath: indexPath)?.contentToken { homeCell.reset() From 2097ee07ef4584ea4544227c414b50c9113b7093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20B=C3=B6ttcher?= Date: Wed, 5 Jan 2022 09:56:06 +0100 Subject: [PATCH 2/2] changed parameter name --- .../src/ViewControllers/HomeView/HVRenderState.swift | 10 +++++----- .../ViewControllers/HomeView/HVTableDataSource.swift | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Signal/src/ViewControllers/HomeView/HVRenderState.swift b/Signal/src/ViewControllers/HomeView/HVRenderState.swift index 75576c6a67..a223eadd14 100644 --- a/Signal/src/ViewControllers/HomeView/HVRenderState.swift +++ b/Signal/src/ViewControllers/HomeView/HVRenderState.swift @@ -42,9 +42,9 @@ public class HVRenderState: NSObject { } @objc - func thread(forIndexPath indexPath: IndexPath, failDebug: Bool = true) -> TSThread? { + func thread(forIndexPath indexPath: IndexPath, expectsSuccess: Bool = true) -> TSThread? { guard let section = HomeViewSection(rawValue: indexPath.section) else { - if failDebug { + if expectsSuccess { owsFailDebug("Invalid section: \(indexPath.section).") } return nil @@ -53,7 +53,7 @@ public class HVRenderState: NSObject { switch section { case .pinned: guard let thread = pinnedThreads[safe: indexPath.row]?.value else { - if failDebug { + if expectsSuccess { owsFailDebug("No thread for index path: \(indexPath)") } return nil @@ -61,14 +61,14 @@ public class HVRenderState: NSObject { return thread case .unpinned: guard let thread = unpinnedThreads[safe: indexPath.row] else { - if failDebug { + if expectsSuccess { owsFailDebug("No thread for index path: \(indexPath)") } return nil } return thread default: - if failDebug { + if expectsSuccess { owsFailDebug("Invalid index path: \(indexPath).") } return nil diff --git a/Signal/src/ViewControllers/HomeView/HVTableDataSource.swift b/Signal/src/ViewControllers/HomeView/HVTableDataSource.swift index 2d39d193dd..d377dd058f 100644 --- a/Signal/src/ViewControllers/HomeView/HVTableDataSource.swift +++ b/Signal/src/ViewControllers/HomeView/HVTableDataSource.swift @@ -85,15 +85,15 @@ extension HVTableDataSource { } @objc - func threadViewModel(forIndexPath indexPath: IndexPath, failDebug: Bool = true) -> ThreadViewModel? { - guard let thread = self.thread(forIndexPath: indexPath, failDebug: failDebug) else { + func threadViewModel(forIndexPath indexPath: IndexPath, expectsSuccess: Bool = true) -> ThreadViewModel? { + guard let thread = self.thread(forIndexPath: indexPath, expectsSuccess: expectsSuccess) else { return nil } return self.threadViewModel(forThread: thread) } - func thread(forIndexPath indexPath: IndexPath, failDebug: Bool = true) -> TSThread? { - renderState.thread(forIndexPath: indexPath, failDebug: failDebug) + func thread(forIndexPath indexPath: IndexPath, expectsSuccess: Bool = true) -> TSThread? { + renderState.thread(forIndexPath: indexPath, expectsSuccess: expectsSuccess) } } @@ -769,7 +769,7 @@ extension HVTableDataSource { public func updateVisibleCellContent(at indexPath: IndexPath, for tableView: UITableView) -> Bool { AssertIsOnMainThread() - if let primKey = threadViewModel(forIndexPath: indexPath, failDebug: false)?.threadRecord.uniqueId, (tableView.indexPathsForVisibleRows ?? []).contains(indexPath) { + if let primKey = threadViewModel(forIndexPath: indexPath, expectsSuccess: false)?.threadRecord.uniqueId, (tableView.indexPathsForVisibleRows ?? []).contains(indexPath) { for cell in tableView.visibleCells { if let homeCell = cell as? HomeViewCell, let myKey = homeCell.thread?.uniqueId, myKey == primKey, let token = buildCellConfigurationAndContentTokenSync(forIndexPath: indexPath)?.contentToken { homeCell.reset()