Add uniquedObjects to ObjectCache.
This commit is contained in:
parent
32ae8ab1d8
commit
031617809f
|
@ -58,9 +58,9 @@ final class StatusesTable: DatabaseTable {
|
|||
let statuses = self.fetchStatusesForArticleIDs(articleIDs, database: database)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
|
||||
self.cacheStatuses(statuses)
|
||||
|
||||
|
||||
cache.addObjectsNotCached(Array(statuses))
|
||||
|
||||
let newArticleIDs = self.articleIDsMissingStatuses(articleIDs)
|
||||
self.createStatusForNewArticleIDs(newArticleIDs)
|
||||
callback()
|
||||
|
@ -155,7 +155,7 @@ private extension StatusesManager {
|
|||
let statuses = articleIDs.map { (oneArticleID) -> LocalArticleStatus in
|
||||
return ArticleStatus(articleID: oneArticleID, read: false, starred: false, userDeleted: false, dateArrived: now)
|
||||
}
|
||||
cacheStatuses(Set(statuses))
|
||||
cache.addObjectsNotCached(statuses)
|
||||
|
||||
queue.update { (database: FMDatabase!) -> Void in
|
||||
|
||||
|
@ -168,28 +168,11 @@ private extension StatusesManager {
|
|||
}
|
||||
}
|
||||
|
||||
// MARK: Cache
|
||||
|
||||
func cacheStatus(_ status: ArticleStatus) {
|
||||
|
||||
cacheStatuses(Set([status]))
|
||||
}
|
||||
|
||||
func cacheStatuses(_ statuses: Set<ArticleStatus>) {
|
||||
|
||||
statuses.forEach { (oneStatus) in
|
||||
if let _ = cachedStatuses[oneStatus.articleID] {
|
||||
return
|
||||
}
|
||||
cachedStatuses[oneStatus.articleID] = oneStatus
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Utilities
|
||||
|
||||
func articleIDsMissingStatuses(_ articleIDs: Set<String>) -> Set<String> {
|
||||
|
||||
return Set(articleIDs.filter { cachedStatusForArticleID($0) == nil })
|
||||
return Set(articleIDs.filter { cache[$0] == nil })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,12 @@ public final class ObjectCache<T> {
|
|||
|
||||
public func addObjects(_ objects: [T]) {
|
||||
|
||||
objects.forEach { addObject($0) }
|
||||
objects.forEach { add($0) }
|
||||
}
|
||||
|
||||
public func addObjectsNotCached(_ objects: [T]) {
|
||||
|
||||
objects.forEach { addIfNotCached($0) }
|
||||
}
|
||||
|
||||
public func add(_ object: T) {
|
||||
|
@ -29,6 +34,15 @@ public final class ObjectCache<T> {
|
|||
self[identifier] = object
|
||||
}
|
||||
|
||||
public func addIfNotCached(_ object: T) {
|
||||
|
||||
let identifier = identifierForObject(object)
|
||||
if let _ = self[identifier] {
|
||||
return
|
||||
}
|
||||
self[identifier] = object
|
||||
}
|
||||
|
||||
public func removeObjects(_ objects: [T]) {
|
||||
|
||||
objects.forEach { removeObject($0) }
|
||||
|
@ -40,12 +54,29 @@ public final class ObjectCache<T> {
|
|||
self[identifier] = nil
|
||||
}
|
||||
|
||||
public func uniquedObjects(_ objects: [T]) -> [T] {
|
||||
|
||||
// Return cached version of each object.
|
||||
// When an object is not already cached, cache it,
|
||||
// then consider that version the unique version.
|
||||
|
||||
return objects.map({ (object) -> T in
|
||||
|
||||
let identifier = identifierForObject(object)
|
||||
if let cachedObject = self[identifier] {
|
||||
return cachedObject
|
||||
}
|
||||
add(object)
|
||||
return object
|
||||
})
|
||||
}
|
||||
|
||||
public subscript(_ identifier: String) -> T? {
|
||||
get {
|
||||
return dictionary[identifier]
|
||||
}
|
||||
set {
|
||||
dictionary[identifier] = T
|
||||
dictionary[identifier] = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue