2017-07-03 20:20:14 +02:00
|
|
|
//
|
2017-07-29 21:08:10 +02:00
|
|
|
// ArticlesTable.swift
|
2017-07-03 20:20:14 +02:00
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 5/9/16.
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Data
|
|
|
|
|
2017-07-29 21:08:10 +02:00
|
|
|
final class ArticlesTable: DatabaseTable {
|
|
|
|
|
|
|
|
let name: String
|
2017-07-29 21:13:38 +02:00
|
|
|
let queue: RSDatabaseQueue
|
2017-07-29 21:08:10 +02:00
|
|
|
|
2017-07-29 21:13:38 +02:00
|
|
|
init(name: String, queue: RSDatabaseQueue) {
|
2017-07-29 21:08:10 +02:00
|
|
|
|
|
|
|
self.name = name
|
2017-07-29 21:13:38 +02:00
|
|
|
self.queue = queue
|
2017-07-29 21:08:10 +02:00
|
|
|
}
|
2017-07-03 20:20:14 +02:00
|
|
|
|
|
|
|
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) }
|
|
|
|
}
|
|
|
|
}
|