Make Articles module adhere to strict Swift concurrency.

This commit is contained in:
Brent Simmons
2024-03-13 20:44:25 -07:00
parent 1ddbe76653
commit 3e6759c29a
4 changed files with 41 additions and 30 deletions

View File

@@ -2,22 +2,26 @@
import PackageDescription
let package = Package(
name: "Articles",
name: "Articles",
platforms: [.macOS(.v14), .iOS(.v17)],
products: [
.library(
name: "Articles",
products: [
.library(
name: "Articles",
type: .dynamic,
targets: ["Articles"]),
],
dependencies: [
targets: ["Articles"]),
],
dependencies: [
.package(url: "https://github.com/Ranchero-Software/RSCore.git", .upToNextMinor(from: "1.0.0")),
],
targets: [
.target(
name: "Articles",
],
targets: [
.target(
name: "Articles",
dependencies: [
"RSCore"
]),
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
]
),
]
)

View File

@@ -53,7 +53,7 @@ public struct Article: Hashable {
}
public static func calculatedArticleID(feedID: String, uniqueID: String) -> String {
return databaseIDWithString("\(feedID) \(uniqueID)")
return DatabaseIDCache.shared.databaseIDWithString("\(feedID) \(uniqueID)")
}
// MARK: - Hashable

View File

@@ -33,7 +33,7 @@ public struct Author: Codable, Hashable {
s += url ?? ""
s += avatarURL ?? ""
s += emailAddress ?? ""
self.authorID = databaseIDWithString(s)
self.authorID = DatabaseIDCache.shared.databaseIDWithString(s)
}
}

View File

@@ -9,23 +9,30 @@
import Foundation
import RSCore
// MD5 works because:
// * Its fast
// * Collisions arent going to happen with feed data
class DatabaseIDCache: @unchecked Sendable {
private var databaseIDCache = [String: String]()
private var databaseIDCacheLock = NSLock()
public func databaseIDWithString(_ s: String) -> String {
databaseIDCacheLock.lock()
defer {
databaseIDCacheLock.unlock()
}
if let identifier = databaseIDCache[s] {
static let shared = DatabaseIDCache()
private var databaseIDCache = [String: String]()
private let databaseIDCacheLock = NSLock()
/// Generates or retrieves from cache  a database-suitable ID based on a String.
func databaseIDWithString(_ s: String) -> String {
databaseIDCacheLock.lock()
defer {
databaseIDCacheLock.unlock()
}
if let identifier = databaseIDCache[s] {
return identifier
}
// MD5 works because:
// * Its fast
// * Collisions arent going to happen with feed data
let identifier = s.md5String
databaseIDCache[s] = identifier
return identifier
}
let identifier = s.md5String
databaseIDCache[s] = identifier
return identifier
}