From 03b43e2e61e95a872b7e83dfd4db8eb01f02814d Mon Sep 17 00:00:00 2001 From: Daniil Vinogradov Date: Fri, 22 Nov 2024 21:10:23 +0100 Subject: [PATCH] Fix trackers list out of bounds --- .../TorrentTrackers/TorrentTrackersViewModel.swift | 2 +- iTorrent/Utils/NSUserDefaultItem.swift | 9 +++++---- iTorrent/Utils/UserDefaultItem.swift | 9 ++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/iTorrent/Screens/TorrentTrackers/TorrentTrackersViewModel.swift b/iTorrent/Screens/TorrentTrackers/TorrentTrackersViewModel.swift index 4c0ae24b..508fef7e 100644 --- a/iTorrent/Screens/TorrentTrackers/TorrentTrackersViewModel.swift +++ b/iTorrent/Screens/TorrentTrackers/TorrentTrackersViewModel.swift @@ -94,7 +94,7 @@ private extension TorrentTrackersViewModel { var newTrackers: [TrackerCellViewModel] = [] var trackerListChanged = false - for tracker in torrentHandle.trackers { + for tracker in torrentHandle.snapshot.trackers { if let oldTracker = trackers.first(where: { $0.url == tracker.trackerUrl }) { oldTracker.update(with: tracker) newTrackers.append(oldTracker) diff --git a/iTorrent/Utils/NSUserDefaultItem.swift b/iTorrent/Utils/NSUserDefaultItem.swift index 6c8814ad..d67a3810 100644 --- a/iTorrent/Utils/NSUserDefaultItem.swift +++ b/iTorrent/Utils/NSUserDefaultItem.swift @@ -48,10 +48,11 @@ private extension NSUserDefaultItem { return try? NSKeyedUnarchiver.unarchivedObject(ofClass: Value.self, from: data) } - static func setValue(_ value: Value, for key: String) { - do { - let data = try NSKeyedArchiver.archivedData(withRootObject: value, requiringSecureCoding: true) + static func setValue(_ value: Value?, for key: String) { + if let value, let data = try? NSKeyedArchiver.archivedData(withRootObject: value, requiringSecureCoding: true) { userDefaults.set(data, forKey: key) - } catch {} + } else { + userDefaults.set(nil, forKey: key) + } } } diff --git a/iTorrent/Utils/UserDefaultItem.swift b/iTorrent/Utils/UserDefaultItem.swift index 7268f44e..b70d3aaa 100644 --- a/iTorrent/Utils/UserDefaultItem.swift +++ b/iTorrent/Utils/UserDefaultItem.swift @@ -43,15 +43,14 @@ private extension UserDefaultItem { static var userDefaults: UserDefaults { .itorrentGroup } static func value(for key: String) -> Value? { - guard let decoded = userDefaults.data(forKey: key), - let res = try? JSONDecoder().decode(Value.self, from: decoded) + guard let data = userDefaults.data(forKey: key) else { return nil } - return res + return try? JSONDecoder().decode(Value.self, from: data) } static func setValue(_ value: Value?, for key: String) { - if let value, let encodedData: Data = try? JSONEncoder().encode(value) { - userDefaults.set(encodedData, forKey: key) + if let value, let data: Data = try? JSONEncoder().encode(value) { + userDefaults.set(data, forKey: key) } else { userDefaults.set(nil, forKey: key) }