From 1b2be5be0e3570f79e1bc14a78af734eb1d8157f Mon Sep 17 00:00:00 2001 From: Andrew Bulhak Date: Mon, 27 May 2024 18:09:48 +0200 Subject: [PATCH 1/2] Retrieve access method items by ID rather than position --- .../APIAccess/List/ListAccessMethodViewController.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ios/MullvadVPN/Coordinators/Settings/APIAccess/List/ListAccessMethodViewController.swift b/ios/MullvadVPN/Coordinators/Settings/APIAccess/List/ListAccessMethodViewController.swift index 2f32ddf1a90d..f85807d67a18 100644 --- a/ios/MullvadVPN/Coordinators/Settings/APIAccess/List/ListAccessMethodViewController.swift +++ b/ios/MullvadVPN/Coordinators/Settings/APIAccess/List/ListAccessMethodViewController.swift @@ -180,7 +180,9 @@ class ListAccessMethodViewController: UIViewController, UITableViewDelegate { itemIdentifier: ListAccessMethodItemIdentifier ) -> UITableViewCell { let cell = tableView.dequeueReusableView(withIdentifier: CellReuseIdentifier.default, for: indexPath) - let item = fetchedItems[indexPath.row] + guard let item = fetchedItems.first(where: { $0.id == itemIdentifier.id }) else { + fatalError("Unable to find item in fetchedItems") + } var contentConfiguration = ListCellContentConfiguration() contentConfiguration.text = item.name From c4d789afa27f4e3ed6517e3cab1eb066c4575123 Mon Sep 17 00:00:00 2001 From: Andrew Bulhak Date: Tue, 28 May 2024 14:10:12 +0200 Subject: [PATCH 2/2] Replace ID search with reloading items only if not deleted --- .../List/ListAccessMethodViewController.swift | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ios/MullvadVPN/Coordinators/Settings/APIAccess/List/ListAccessMethodViewController.swift b/ios/MullvadVPN/Coordinators/Settings/APIAccess/List/ListAccessMethodViewController.swift index f85807d67a18..474baea1f7fd 100644 --- a/ios/MullvadVPN/Coordinators/Settings/APIAccess/List/ListAccessMethodViewController.swift +++ b/ios/MullvadVPN/Coordinators/Settings/APIAccess/List/ListAccessMethodViewController.swift @@ -158,6 +158,7 @@ class ListAccessMethodViewController: UIViewController, UITableViewDelegate { } private func updateDataSource(animated: Bool = true) { + guard let dataSource else { return } fetchedItems = interactor.fetch() var snapshot = NSDiffableDataSourceSnapshot() @@ -168,11 +169,13 @@ class ListAccessMethodViewController: UIViewController, UITableViewDelegate { } snapshot.appendItems(itemIdentifiers, toSection: .primary) - for item in fetchedItems { - snapshot.reloadItems([ListAccessMethodItemIdentifier(id: item.id)]) + if dataSource.snapshot().numberOfItems == fetchedItems.count { + for item in fetchedItems { + snapshot.reloadItems([ListAccessMethodItemIdentifier(id: item.id)]) + } } - dataSource?.apply(snapshot, animatingDifferences: animated) + dataSource.apply(snapshot, animatingDifferences: animated) } private func dequeueCell( @@ -180,9 +183,7 @@ class ListAccessMethodViewController: UIViewController, UITableViewDelegate { itemIdentifier: ListAccessMethodItemIdentifier ) -> UITableViewCell { let cell = tableView.dequeueReusableView(withIdentifier: CellReuseIdentifier.default, for: indexPath) - guard let item = fetchedItems.first(where: { $0.id == itemIdentifier.id }) else { - fatalError("Unable to find item in fetchedItems") - } + let item = fetchedItems[indexPath.row] var contentConfiguration = ListCellContentConfiguration() contentConfiguration.text = item.name