mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2025-01-14 10:36:03 +01:00
Save to disk when needed. Coalesce writing, so it doesn’t happen over and over during a refresh session.
This commit is contained in:
parent
bed43dd3d6
commit
ade76d6405
@ -44,6 +44,24 @@ public final class Account: DisplayNameProvider, Hashable {
|
||||
var topLevelObjects = [AnyObject]()
|
||||
var feedIDDictionary = [String: Feed]()
|
||||
var username: String?
|
||||
var saveTimer: Timer?
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
else if !dirty {
|
||||
saveTimer?.rs_invalidateIfValid()
|
||||
saveTimer = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var refreshInProgress = false {
|
||||
didSet {
|
||||
@ -237,6 +255,44 @@ extension Account {
|
||||
}
|
||||
return Folder(account: self, dictionary: diskObject)
|
||||
}
|
||||
|
||||
func saveToDiskIfNeeded() {
|
||||
|
||||
if dirty {
|
||||
saveToDisk()
|
||||
}
|
||||
}
|
||||
|
||||
private func diskDictionary() -> NSDictionary {
|
||||
|
||||
let diskObjects = topLevelObjects.flatMap { (object) -> [String: Any]? in
|
||||
|
||||
if let folder = object as? Folder {
|
||||
return folder.dictionary
|
||||
}
|
||||
else if let feed = object as? Feed {
|
||||
return feed.dictionary
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var d = [String: Any]()
|
||||
d[Key.children] = diskObjects as NSArray
|
||||
return d as NSDictionary
|
||||
}
|
||||
|
||||
func saveToDisk() {
|
||||
|
||||
let d = diskDictionary()
|
||||
do {
|
||||
try RSPlist.write(d, filePath: settingsFile)
|
||||
}
|
||||
catch let error as NSError {
|
||||
NSApplication.shared.presentError(error)
|
||||
}
|
||||
|
||||
dirty = false
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
Loading…
Reference in New Issue
Block a user