Stop storing unreadCount for Feed and Folder objects on disk, since they’re fast to fetch at startup. Also, give Feed an ODBRawValueTable for metadata storage.

This commit is contained in:
Brent Simmons 2018-09-13 22:25:10 -07:00
parent ce3a90c518
commit b5b42b8df6
3 changed files with 19 additions and 38 deletions

View File

@ -60,6 +60,8 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
private let settingsODB: ODB
private let settingsTable: ODBTable
private let feedsPath: ODBPath
private let feedsTable: ODBTable
private struct SettingsKey {
static let unreadCount = "unreadCount"
@ -122,6 +124,8 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
self.settingsODB.vacuum()
let settingsPath = ODBPath.path(["settings"])
self.settingsTable = settingsODB.ensureTable(settingsPath)!
self.feedsPath = ODBPath.path(["feeds"])
self.feedsTable = settingsODB.ensureTable(self.feedsPath)!
NotificationCenter.default.addObserver(self, selector: #selector(downloadProgressDidChange(_:)), name: .DownloadProgressDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
@ -134,7 +138,6 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
pullObjectsFromDisk()
DispatchQueue.main.async {
self.updateUnreadCount()
self.fetchAllUnreadCounts()
}
@ -439,27 +442,9 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
@objc func unreadCountDidChange(_ note: Notification) {
// Update the unread count if its a direct child.
// If the object is owned by this account, then mark dirty
// since unread counts are saved to disk along with other feed info.
if let object = note.object {
if objectIsChild(object as AnyObject) {
updateUnreadCount()
self.dirty = true
return
}
if let feed = object as? Feed {
if feed.account === self {
self.dirty = true
}
}
if let folder = object as? Folder {
if folder.account === self {
self.dirty = true
}
}
if let object = note.object, objectIsChild(object as AnyObject) {
updateUnreadCount()
}
}
@ -529,6 +514,12 @@ extension Account {
return diskObjects.compactMap { object(with: $0) }
}
func settingsTableForFeed(feedID: String) -> ODBRawValueTable? {
let feedPath = feedsPath + feedID
let table = settingsODB.ensureTable(feedPath)
return table?.rawValueTable
}
}
// MARK: - Disk (Private)
@ -713,6 +704,7 @@ private extension Account {
feed.unreadCount = 0
}
}
self.updateUnreadCount()
}
}
}

View File

@ -10,6 +10,7 @@ import Foundation
import RSCore
import RSWeb
import Articles
import RSDatabase
public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable {
@ -68,6 +69,10 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable {
}
}
private lazy var settingsTable: ODBRawValueTable? = {
return account?.settingsTableForFeed(feedID: feedID)
}()
// MARK: - Init
public init(accountID: String, url: String, feedID: String) {
@ -90,7 +95,6 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable {
static let authors = "authors"
static let conditionalGetInfo = "conditionalGetInfo"
static let contentHash = "contentHash"
static let unreadCount = "unreadCount"
}
convenience public init?(accountID: String, dictionary: [String: Any]) {
@ -112,10 +116,6 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable {
self.conditionalGetInfo = HTTPConditionalGetInfo(dictionary: conditionalGetInfoDictionary)
}
if let savedUnreadCount = dictionary[Key.unreadCount] as? Int {
self.unreadCount = savedUnreadCount
}
if let authorsDiskArray = dictionary[Key.authors] as? [[String: Any]] {
self.authors = Author.authorsWithDiskArray(authorsDiskArray)
}
@ -157,9 +157,6 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable {
if let contentHash = contentHash {
d[Key.contentHash] = contentHash
}
if unreadCount > 0 {
d[Key.unreadCount] = unreadCount
}
if let conditionalGetInfo = conditionalGetInfo {
d[Key.conditionalGetInfo] = conditionalGetInfo.dictionary
}

View File

@ -60,7 +60,6 @@ public final class Folder: DisplayNameProvider, Container, UnreadCountProvider,
private struct Key {
static let name = "name"
static let unreadCount = "unreadCount"
static let children = "children"
}
@ -72,10 +71,6 @@ public final class Folder: DisplayNameProvider, Container, UnreadCountProvider,
if let childrenArray = dictionary[Key.children] as? [[String: Any]] {
self.children = Folder.objects(with: childrenArray, account: account)
}
if let savedUnreadCount = dictionary[Key.unreadCount] as? Int {
self.unreadCount = savedUnreadCount
}
}
var dictionary: [String: Any] {
@ -88,10 +83,7 @@ public final class Folder: DisplayNameProvider, Container, UnreadCountProvider,
if let name = name {
d[Key.name] = name
}
if unreadCount > 0 {
d[Key.unreadCount] = unreadCount
}
let childObjects = children.compactMap { (child) -> [String: Any]? in
if let feed = child as? Feed {