From 0ba557736e85aef8221506d1a8b876230c710674 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 7 Oct 2017 20:11:17 -0700 Subject: [PATCH] Avoid saving account to disk during a refresh session. Reschedule the timer and do it later. --- Frameworks/Account/Account.swift | 86 ++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 33 deletions(-) diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 71b032f84..e5b75f7ef 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -49,16 +49,10 @@ public final class Account: DisplayNameProvider, Hashable { private var dirty = false { didSet { if dirty { - saveTimer?.rs_invalidateIfValid() - saveTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { (timer) in - self.saveToDiskIfNeeded() - timer.rs_invalidateIfValid() - self.saveTimer = nil - } + resetSaveTimer() } - else if !dirty { - saveTimer?.rs_invalidateIfValid() - saveTimer = nil + else { + removeSaveTimer() } } } @@ -222,14 +216,32 @@ public final class Account: DisplayNameProvider, Hashable { } -// MARK: - Disk +// MARK: - Disk (Public) extension Account { + + func objects(with diskObjects: [[String: Any]]) -> [AnyObject] { + + return diskObjects.flatMap { object(with: $0) } + } +} + +// MARK: - Disk (Private) + +private extension Account { - private struct Key { + struct Key { static let children = "children" } + func object(with diskObject: [String: Any]) -> AnyObject? { + + if Feed.isFeedDictionary(diskObject) { + return Feed(accountID: accountID, dictionary: diskObject) + } + return Folder(account: self, dictionary: diskObject) + } + func pullObjectsFromDisk() { let settingsFileURL = URL(fileURLWithPath: settingsFile) @@ -243,27 +255,7 @@ extension Account { updateFeedIDDictionary() } - func objects(with diskObjects: [[String: Any]]) -> [AnyObject] { - - return diskObjects.flatMap { object(with: $0) } - } - - func object(with diskObject: [String: Any]) -> AnyObject? { - - if Feed.isFeedDictionary(diskObject) { - return Feed(accountID: accountID, dictionary: diskObject) - } - return Folder(account: self, dictionary: diskObject) - } - - func saveToDiskIfNeeded() { - - if dirty { - saveToDisk() - } - } - - private func diskDictionary() -> NSDictionary { + func diskDictionary() -> NSDictionary { let diskObjects = topLevelObjects.flatMap { (object) -> [String: Any]? in @@ -281,6 +273,21 @@ extension Account { return d as NSDictionary } + func saveToDiskIfNeeded() { + + if !dirty { + return + } + + if refreshInProgress { + resetSaveTimer() + return + } + + saveToDisk() + dirty = false + } + func saveToDisk() { let d = diskDictionary() @@ -290,8 +297,21 @@ extension Account { catch let error as NSError { NSApplication.shared.presentError(error) } + } - dirty = false + func resetSaveTimer() { + + saveTimer?.rs_invalidateIfValid() + + saveTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { (timer) in + self.saveToDiskIfNeeded() + } + } + + func removeSaveTimer() { + + saveTimer?.rs_invalidateIfValid() + saveTimer = nil } }