NetNewsWire/Frameworks/Database/ArticlesTable.swift

62 lines
1.3 KiB
Swift
Raw Normal View History

//
// ArticlesTable.swift
// Evergreen
//
// Created by Brent Simmons on 5/9/16.
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import Data
final class ArticlesTable: DatabaseTable {
let name: String
2017-07-29 21:13:38 +02:00
let queue: RSDatabaseQueue
2017-07-29 21:13:38 +02:00
init(name: String, queue: RSDatabaseQueue) {
self.name = name
2017-07-29 21:13:38 +02:00
self.queue = queue
}
private let cachedArticles: NSMapTable<NSString, Article> = NSMapTable.weakToWeakObjects()
func uniquedArticles(_ fetchedArticles: Set<Article>, statusesManager: StatusesManager) -> Set<Article> {
var articles = Set<Article>()
for oneArticle in fetchedArticles {
assert(oneArticle.status != nil)
if let existingArticle = cachedArticle(oneArticle.articleID) {
articles.insert(existingArticle)
}
else {
cacheArticle(oneArticle)
articles.insert(oneArticle)
}
}
statusesManager.attachCachedStatuses(articles)
return articles
}
func cachedArticle(_ articleID: String) -> Article? {
return cachedArticles.object(forKey: articleID as NSString)
}
func cacheArticle(_ article: Article) {
cachedArticles.setObject(article, forKey: article.articleID as NSString)
}
func cacheArticles(_ articles: Set<Article>) {
articles.forEach { cacheArticle($0) }
}
}