NetNewsWire/Frameworks/Database/AuthorsTable.swift

49 lines
969 B
Swift
Raw Normal View History

2017-07-13 22:38:47 +02:00
//
// AuthorsTable.swift
2017-07-13 22:38:47 +02:00
// Database
//
// Created by Brent Simmons on 7/13/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import Foundation
2017-08-04 06:10:01 +02:00
import RSDatabase
2017-07-13 22:38:47 +02:00
import Data
final class AuthorsTable: DatabaseTable {
let name: String
2017-07-29 21:13:38 +02:00
let queue: RSDatabaseQueue
2017-08-04 06:10:01 +02:00
private let cache = ObjectCache<Author>(keyPathForID: \Author.databaseID)
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
}
2017-07-13 22:38:47 +02:00
func authorWithRow(_ row: FMResultSet) -> Author? {
2017-08-04 06:10:01 +02:00
// Since:
// 1. anything to do with an FMResultSet runs inside the database serial queue, and
// 2. the cache is referenced only within this method,
// this is safe.
guard let databaseID = row.string(forColumn: DatabaseKey.databaseID) else {
return nil
}
if let cachedAuthor = cache[databaseID] {
return cachedAuthor
2017-07-13 22:38:47 +02:00
}
guard let author = Author(row: row) else {
return nil
}
2017-08-04 06:10:01 +02:00
cache[databaseID] = author
2017-07-13 22:38:47 +02:00
return author
}
}
2017-08-04 06:10:01 +02:00