Avoid saving account to disk during a refresh session. Reschedule the timer and do it later.

This commit is contained in:
Brent Simmons 2017-10-07 20:11:17 -07:00
parent d8e723db46
commit 0ba557736e
1 changed files with 53 additions and 33 deletions

View File

@ -49,16 +49,10 @@ public final class Account: DisplayNameProvider, Hashable {
private var dirty = false { private var dirty = false {
didSet { didSet {
if dirty { if dirty {
saveTimer?.rs_invalidateIfValid() resetSaveTimer()
saveTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { (timer) in
self.saveToDiskIfNeeded()
timer.rs_invalidateIfValid()
self.saveTimer = nil
} }
} else {
else if !dirty { removeSaveTimer()
saveTimer?.rs_invalidateIfValid()
saveTimer = nil
} }
} }
} }
@ -222,14 +216,32 @@ public final class Account: DisplayNameProvider, Hashable {
} }
// MARK: - Disk // MARK: - Disk (Public)
extension Account { extension Account {
private struct Key { func objects(with diskObjects: [[String: Any]]) -> [AnyObject] {
return diskObjects.flatMap { object(with: $0) }
}
}
// MARK: - Disk (Private)
private extension Account {
struct Key {
static let children = "children" 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() { func pullObjectsFromDisk() {
let settingsFileURL = URL(fileURLWithPath: settingsFile) let settingsFileURL = URL(fileURLWithPath: settingsFile)
@ -243,27 +255,7 @@ extension Account {
updateFeedIDDictionary() updateFeedIDDictionary()
} }
func objects(with diskObjects: [[String: Any]]) -> [AnyObject] { func diskDictionary() -> NSDictionary {
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 {
let diskObjects = topLevelObjects.flatMap { (object) -> [String: Any]? in let diskObjects = topLevelObjects.flatMap { (object) -> [String: Any]? in
@ -281,6 +273,21 @@ extension Account {
return d as NSDictionary return d as NSDictionary
} }
func saveToDiskIfNeeded() {
if !dirty {
return
}
if refreshInProgress {
resetSaveTimer()
return
}
saveToDisk()
dirty = false
}
func saveToDisk() { func saveToDisk() {
let d = diskDictionary() let d = diskDictionary()
@ -290,8 +297,21 @@ extension Account {
catch let error as NSError { catch let error as NSError {
NSApplication.shared.presentError(error) 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
} }
} }