From d40d7757176e0e7e536856bd70f50f254756dd9b Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Mon, 22 Jun 2026 14:33:12 +0300 Subject: [PATCH] Don't crash when scrolling to the oldest item before the data is applied scrollToOldestItem targeted (timelineItemsIDs.count - 1, section: 1) even when the table's data source hadn't applied that section yet, raising an out-of-bounds NSRangeException. Scroll against the table's actual section and row counts instead. --- .../Screens/Timeline/TimelineTableViewController.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ElementX/Sources/Screens/Timeline/TimelineTableViewController.swift b/ElementX/Sources/Screens/Timeline/TimelineTableViewController.swift index 1a8bcce67..8063e60d1 100644 --- a/ElementX/Sources/Screens/Timeline/TimelineTableViewController.swift +++ b/ElementX/Sources/Screens/Timeline/TimelineTableViewController.swift @@ -420,10 +420,13 @@ class TimelineTableViewController: UIViewController { /// Scrolls to the oldest item in the timeline. private func scrollToOldestItem(animated: Bool) { - guard !timelineItemsIDs.isEmpty else { + // The data source can lag behind timelineItemsIDs, so scroll against the table's actual + // contents to avoid targeting a section or row that doesn't exist yet. + guard tableView.numberOfSections > 1, + tableView.numberOfRows(inSection: 1) > 0 else { return } - tableView.scrollToRow(at: IndexPath(item: timelineItemsIDs.count - 1, section: 1), at: .bottom, animated: animated) + tableView.scrollToRow(at: IndexPath(item: tableView.numberOfRows(inSection: 1) - 1, section: 1), at: .bottom, animated: animated) scrollDirectionPublisher.send(.top) }